{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1 变分法(Variational Method, Calculus of Variations)\n",
    "    变分法是处理泛函极值的数学工具. \n",
    "变分法是 17 世纪末发展起来的数学分析的一个分支. 它是研究某些未知函数的积分型泛函极值的普遍方法. \n",
    "简言之，求泛函极值的方法称为变分法，求泛函极值的问题称为变分问题. \n",
    "\n",
    "变分法是泛函分析的一个重要组成部分，但变分法出现在前，泛函分析出现在后. \n",
    "\n",
    "- 这里只讨论“**将泛函极值问题转化为微分方程的边值问题**”这样的一种思路【经典变分法】；\n",
    "- 反过来，“将已知微分方程的边值问题变成一个具有等价解的变分问题”（求BVP的弱解），\n",
    "这种**微分方程的变分方法**作为另一个体系，被称为【变分原理】. （因为求解微分方程的边值问题也是十分困难的，所以就产生了变分学中的【直接方法】，就是不通过欧拉方程而直接近似求解变分问题的方法.）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-02-03T16:45:45+08:00",
     "start_time": "2021-02-03T08:45:45.267Z"
    }
   },
   "outputs": [],
   "source": [
    "<<VariationalMethods`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.1 预备知识\n",
    "    泛函(Functional)的概念\n",
    "在讨论泛函极值之前，我们需要先把泛函弄清楚. \n",
    "\n",
    "### 1.1.1 泛函\n",
    "**定义**【泛函】\n",
    "假设对某一类函数 $\\{u(x)\\}$ 中的每一个函数 $u(x)$ 有一个实数值 $\\mathcal{A}$ 与之对应，那么变量 $\\mathcal{A}$ 称为定义于 \n",
    "$\\{u(x)\\}$ 上的泛函，并记为\n",
    "$$\n",
    "\\mathcal{A}=\\mathcal{A}[u(x)]\\ \\text{或}\\ \\mathcal{A}[u(\\cdot)]\\ \\text{或}\\ \\mathcal{A}[u].\n",
    "$$\n",
    "\n",
    "泛函是从一组函数（函数空间）到实数的一个映射. 也就是“自变量为函数的实值函数”. \n",
    "上述定义不难推广到依赖于多个函数的泛函，也不难推广到定义在多元函数上的泛函. 这时，上述定义中的 $x,u$ \n",
    "均可以是矢量，甚至是张量. \n",
    "- 类似于函数的定义域，求解变分问题要在指定的函数类中进行，\n",
    "这个函数类叫作变分问题的**容许函数类**(admissible function class). \n",
    "或称为**可取函数**的集合. \n",
    "一般记作 $X$（$\\mathfrak{X}$）. 也有记作 $A$ 的. \n",
    "- 我们熟悉的傅里叶系数\n",
    "$$\n",
    "\\begin{aligned}\n",
    "a_n&=\\mathcal{I}[f(\\cdot)]=\\frac1\\ell\\int_{-\\ell}^{\\ell} f(x)\\cos\\frac{n\\pi x}\\ell \n",
    "\\mathrm{d}x\\\\\n",
    "b_n&=\\mathcal{J}[f(\\cdot)]=\\frac1\\ell\\int_{-\\ell}^{\\ell} f(x)\\sin\\frac{n\\pi x}\\ell \n",
    "\\mathrm{d}x\n",
    "\\end{aligned}\n",
    "$$\n",
    "都是泛函，它把“满足狄利克雷条件”以 $2\\ell$ 为周期的函数映射为实数. \n",
    "- 通常在变分法中，泛函表示为定积分的形式，并涉及函数及其导函数. \n",
    "如最速降线问题的时间作为路径的泛函，有表达式：\n",
    "$$\n",
    "T[y(x)]:=\\int_{x_1}^{x_2}\\left[\\frac{1+{y'}^2(x)}{2 g y(x)}\\right]^\\frac12 \\mathrm{d}x \\in\\mathbb{R}. \n",
    "$$\n",
    "- 这里所研究的泛函一般用积分显式表达，并不意味着所有泛函都能用显式积分表达. \n",
    "- 所要研究的泛函都可表示成在一定区间或一定区域内的函数及其导数（或偏导数）的积分形式，如\n",
    "$$\n",
    "\\begin{aligned}\n",
    "\\mathcal{A}_{1}[x(t)] &=\\int_{a}^{b} L\\left(x(t), x^{\\prime}(t), x^{\\prime \\prime}(t) ;\n",
    "t\\right) \\mathrm{d} t \\\\\n",
    "\\mathcal{A}_{2}[u(x,y,z,t)] &=\\iiiint_{\\Omega} L\\left(u(x,y,z,t), u_{x}, u_{y}, u_{z}, u_{t} ;\n",
    "x,y,z,t\\right) \\mathrm{d} x \\mathrm{~d} y \\mathrm{~d} z \\mathrm{~d} t\n",
    "\\end{aligned}\n",
    "$$\n",
    "- 泛函中的可变化函数称为自变函数，或称**宗量**(argument)，如上述泛函 $\\mathcal{A}_2$ 中的函数 $u$. 而其中的 \n",
    "$x,y,z,t$ 仅是积分变量，$\\Omega$ 则是被积函数 $f$ 的定义域. \n",
    "- 泛函的常见符号一般都是大写英文字母，如$I, J$，但我们使用$\\mathcal{A}$（表示物理中的作用量泛函 Action）；\n",
    "被积式的常见符号一般也是大写英文字母，如$F$，但这里我们用$L$（表示物理中的拉格朗日函数 Lagrangian）. \n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.1.2 函数&泛函\n",
    "值得注意的是，与泛函表达式（如 $\\mathcal{A}[x(t)]$）有相同形式的复合函数（如 \n",
    "$f(x(t))$）. \n",
    "虽然它们看上去很像，但它们之间有一个本质上的区别，\n",
    "即复合函数本质上还是依赖于**数**，\n",
    "当自变量 $t$ 给定一个值就能算出一个复合函数 $f(x(t))$ 的值；\n",
    "对应地，泛函则依赖于**函数**，泛函的值既不取决于变量 $t$ \n",
    "的值也不取决于函数 $x(t)$ 的值，而是取决于整个函数 $x(t)$ 和 $x$ \n",
    "的某个区间. \n",
    "一个关键在于泛函中的变量 $t$ 一般都是作为**积分哑元**的，\n",
    "由此可见泛函与复合函数是不同的. \n",
    "下表给出了函数与泛函的对比. \n",
    "\n",
    "<span style=\"color:red\">\n",
    "注\n",
    "    </span>\n",
    "：下面的符号 $T$ 表示线性映射. \n",
    "\n",
    "|            |           函数         |            泛函            |\n",
    "|    :---:    |          :---:            |           :---:              |\n",
    "|  符号表示   |              $$x(t)$$           |      $$\\mathcal{A}[x], x=x(t)$$            |\n",
    "|   映射关系 |     $$\\text{点}\\to\\text{点}$$     |      $$\\text{曲线}\\to\\text{点}$$      |\n",
    "|  导数定义   | $$\\frac{\\mathrm{d}x(t)}{\\mathrm{d}t}=\\lim_{h\\to0}\\frac{x(t+h)-x(t)}{h}$$   | $$\\frac{\\delta \\mathcal{A}[x]}{\\delta x(t)}=\\lim_{\\varepsilon\\to0}\\frac{\\mathcal{A}[x+\\varepsilon\\cdot\\alpha]-\\mathcal{A}[x]}{\\varepsilon}, h=\\varepsilon\\cdot\\alpha$$  |\n",
    "|  另一形式   |$$x(t+h) = x(t)+T(h)+\\varphi(h), $$其中函数$\\varphi$满足$\\lim_{h\\to0} \\frac{\\varphi(h)}{h}=0$     |   $$\\mathcal{A}[x+h]=\\mathcal{A}[x]+T[h]+R[h,x]$$ |\n",
    "| 极值条件 | $$\\frac{\\mathrm{d}x(t)}{\\mathrm{d}t}=T(h)=x'(t)=0$$   |  对于所有的$h=h(t)$，有 $$\\frac{\\delta \\mathcal{A}[x]}{\\delta x(t)}=T[h]=0$$  |\n",
    "\n",
    "常见的函数导数记号有\n",
    "$$\n",
    "\\frac{\\mathrm{d}x(t)}{\\mathrm{d}t}=\n",
    "x'(t)=\\dot{x}(t),\n",
    "$$\n",
    "而多元函数的偏导则有如下记号\n",
    "$$\n",
    "\\frac{\\partial u(x,y,z,t)}{\\partial t}=\n",
    "\\partial_t u=u_t\n",
    "$$\n",
    "\n",
    "---\n",
    "【小增量的线性部分】\n",
    "在导数的这种表述 $f(x+h) = f(x)+T(h)+\\varphi(h)$ 下，\n",
    "我们可以很容易地将函数的微商推广到**多元的向量函数**（从$\\mathbb{R}^n$到$\\mathbb{R}^m$的映射），\n",
    "这里的线性映射 $T$ 就是雅各比矩阵 $\\left(\\partial y^i{\\big/}\\partial x^j\\right)$. \n",
    "\n",
    "---\n",
    "【对参数的微商】\n",
    "从 $f′(x)=\\lim_{h\\to0}\\left[f(x+h)-f(x)\\right]/h$ 这一形式出发，我们可以定义函数 $\\varphi(h)=$\n",
    "$$\n",
    "\\left\\{\\begin{matrix} \n",
    "  f(x+h) − f(x) − f′(x)h & \\text{for} & h\\ne0\\\\  \n",
    "  0 & \\text{for} & h=0\n",
    "\\end{matrix}\\right. \n",
    "$$\n",
    "则，有 $f(x+h) = f(x)+T h+\\varphi(h)$，其中 $T=f'(x)$ 且 $\\lim_{h\\to0}\\varphi(h)/h=0$. \n",
    "反过来，从 $f(x+h) = f(x)+T h+\\varphi(h)$ 开始，就有\n",
    "$$\n",
    "\\frac{f(x+h)-f(x)}{h} = T+\\frac{\\varphi(h)}{h}\n",
    "$$\n",
    "取极限 $h\\to0$，我们可以看到 $f'(x)=T$. 因此，函数导数的这**两种表述是等价的**. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.1.3 泛函极值\n",
    "微积分中的*函数极值必要条件*是函数导数等于零，即函数取到**驻值**. \n",
    "驻值相应的自变量取值为**驻点**或称为**稳定点**，因为自变量的无穷小变不会对函数值造成大影响\n",
    "（因为那点斜率为零）. \n",
    "\n",
    "同样的，下面将会提到的几个例子是关于“路径的无穷小变如何影响一个积分”，\n",
    "对此我们就把研究这类泛函极值的问题的方法称为**变分法(variational methods)**. \n",
    "类似地，我们可以称使得泛函取得驻值的曲线为**稳定路径**. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.2 变分问题的引入&变分法的提出\n",
    "无论是理论研究还是实际应用，其中都广泛存在与泛函极值有关的问题，此处只列举最经典的几例. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.2.1 等周问题(Isoperimetric)\n",
    "    所围面积最大的定长闭曲线\n",
    "平面上的等周问题是最古老的变分问题，它的出现可以追溯到很早以前. \n",
    "虽然其结论很简单，但要严格的证明这一点并不容易. \n",
    "这个问题可以被表述为：在平面上所有周长一定的封闭曲线中，是否有一个围成的面积最大？\n",
    "如果有的话，是什么形状？\n",
    "\n",
    "[![Isoperimetric_inequality](https://en.wikipedia-on-ipfs.org/I/m/Isoperimetric_inequality_illustr2.svg.png)](https://en.wikipedia-on-ipfs.org/wiki/Isoperimetric_inequality.html)\n",
    "(An elongated shape can be made more *round* while keeping its perimeter fixed and increasing its area.)\n",
    "\n",
    "- 另一种等价的表述是：当平面上的封闭曲线围成的面积一定时，怎样的曲线周长最小？\n",
    "- 等周问题有许多不同的推广，例如在各种*曲面*而不是平面上的等周问题，以及在*高维*的空间中给定的“表面”或区域的最大“边界长度”问题. \n",
    "\n",
    "---\n",
    "设所求曲线 $c$ 由如下参数形式给出. \n",
    "$$\n",
    "\\left\\{\n",
    "\\begin{aligned}\n",
    "x&=x(t),\\\\\n",
    "y&=y(t)\n",
    "\\end{aligned}\n",
    "\\quad (t_1\\le t\\le t_2)\n",
    "\\right.\n",
    "$$\n",
    "对于满足等周条件的光滑闭曲线 \n",
    "$$\n",
    "\\Gamma=\\left\\{\n",
    "\\begin{aligned}\n",
    "x&=x(t)\\\\\n",
    "y&=y(t)\n",
    "\\end{aligned}\\in C^1[t_1,t_2]:\n",
    "\\left\\{\\begin{aligned}\n",
    "x(t_1)&=x(t_2)\\\\\n",
    "y(t_1)&=y(t_2)\n",
    "\\end{aligned}\\right. ,\n",
    "\\ell=\\int_{t_1}^{t_2}\\sqrt{\\dot{x}^2+\\dot{y}^2}\\mathrm{d}t\n",
    "\\right\\},\n",
    "$$\n",
    "其中 $\\ell$ 是常数，所围成的面积（由格林公式）\n",
    "$$\n",
    "S[x(\\cdot),y(\\cdot)]=\\frac12\\iint_D 2\\mathrm{d}x\\mathrm{d}y\n",
    "=\\frac12\\int_{t_1}^{t_2}\\left(x\\dot{y}-y\\dot{x}\\right)\\mathrm{d}t\n",
    "$$\n",
    "要最大. 于是，等周问题可叙述为\n",
    "$$\n",
    "\\sup_{c\\in\\Gamma}\\int_{t_1}^{t_2}\\left(x\\dot{y}-y\\dot{x}\\right)\\mathrm{d}t\n",
    "$$\n",
    "\n",
    "更一般地，我们有等周不等式. 假设 $D\\subset\\mathbb{R}^n$ 是一个有界光滑区域，则\n",
    "$$\n",
    "\\left[\\left|\\partial D\\right|\\right]^n-n^{n-1}\\omega_n\n",
    "\\left[\\left| D\\right|\\right]^{n-1}\\ge0,\n",
    "$$\n",
    "其中 $\\omega_n$ 是$\\mathbb{R}^n$ 中的单位球“面积”，\n",
    "$\\left|\\partial D\\right|$, $\\left|D\\right|$ 分别表示 $D$ 的边界“面积”和 $D$ 的“体积”. \n",
    "\n",
    "设 $n=2$，我们描述 \n",
    "$\\partial D=\\left\\{\\boldsymbol{u}(x)\n",
    "=\\left(u^1(x),u^2(x)\\right):x\\in[a,b]\\right\\}$，则\n",
    "$$\n",
    "\\left|\\partial D\\right|(\\boldsymbol{u})=\\int_a^b\\sqrt{\\left|\\dot{\\boldsymbol{u}}\\right|^2}\\mathrm{d}x,\\quad\n",
    "\\left|D\\right|(\\boldsymbol{u})=\\frac12\\int_a^b\\boldsymbol{u}\\wedge\\dot{\\boldsymbol{u}}\\mathrm{~d}x,\n",
    "$$\n",
    "这里 $\\dot{\\boldsymbol{u}}=\\left(\\dot{u}^1, \\dot{u}^2\\right)$，\n",
    "$\\boldsymbol{u}\\wedge\\dot{\\boldsymbol{u}}=\n",
    "u^1\\dot{u}^2 - u^2\\dot{u}^1$. \n",
    "假设 $W=\\left\\{\\boldsymbol{u}:\\left|D\\right|(\\boldsymbol{u})=1, \\boldsymbol{u}(a)=\\boldsymbol{u}(b)\\right\\}$，则相应变分问题的提法为\n",
    "$$\n",
    "\\inf_{\\boldsymbol{u}\\in W}\\int_a^b\\sqrt{\\left|\\dot{\\boldsymbol{u}}\\right|^2}\\mathrm{d}x=2\\sqrt{\\pi}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.2.2 短程线问题(Geodesic)\n",
    "    给定曲面上两点间距离最短的曲线\n",
    "给定平面中两个点，求其**最短路径**. 显然答案是直线，但是证明就要用变分法才行. \n",
    "先找到路径长度的表达式，弧微分作曲线积分，再换元，变成一个定积分；\n",
    "路径长度表达式含有一个未知函数，要使得积分取值最小就得... （用到变分法）\n",
    "\n",
    "- 更一般的情况就是求在曲面上给定两点之间最短曲线的方程. 或称为*测地线问题*. \n",
    "- 由微分几何，曲面 $\\Sigma$ 上短程曲率恒等于零的曲线称为 $\\Sigma$ 上的短程线. 如，\n",
    "    - 柱面上的短程线就是它上面的螺线（包括母线和它们的正交轨线）. \n",
    "    - 球面上的短程线就是它上面的大圆的劣弧. \n",
    "\n",
    "[![Geodesic](https://en.wikipedia-on-ipfs.org/I/m/Spherical_triangle.svg.png)](https://en.wikipedia-on-ipfs.org/wiki/Geodesic.html)\n",
    "(A geodesic triangle on the sphere. The geodesics are *great circle* arcs.)\n",
    "\n",
    "- 在广义相对论中，一个不受约束的粒子是在一条短程线上运动的. \n",
    "\n",
    "---\n",
    "已知光滑曲面 $S: G(x,y,z)=0$ 和位于该曲面上的两点\n",
    "$A(x_1,y_1,z_1)$ 和 $B(x_2,y_2,z_2)$，在曲面上连接 \n",
    "$A,B$ 的可求长曲线无穷多，对任一可求长曲线都有相应的弧长. \n",
    "设曲面 $S$ 上连接 $A,B$ 的一阶光滑曲线是\n",
    "$$\n",
    "\\left\\{\n",
    "\\begin{aligned}\n",
    "y&=y(x),\\\\\n",
    "z&=z(x)\n",
    "\\end{aligned}\n",
    "\\quad (x_1\\le x\\le x_2)\n",
    "\\right.\n",
    "$$\n",
    "弧 ${AB}$ 的长度为\n",
    "$$\n",
    "s[y(\\cdot),z(\\cdot)]=\\int_A^B\\mathrm{d}s=\n",
    "\\int_{x_1}^{x_2}\\sqrt{1+\\left(\\frac{\\mathrm{d}y}\n",
    "{\\mathrm{d}x}\\right)^2+\\left(\\frac{\\mathrm{d}z}\n",
    "{\\mathrm{d}x}\\right)^2}\\mathrm{d}x.\n",
    "$$\n",
    "\n",
    "若这张曲面 $S$ 用参数方程表示为 $x=x(u,v), y=y(u,v), z=z(u,v)$，并记 $E=x_u^2+y_u^2+z_u^2$, \n",
    "$F=x_ux_v+y_uy_v+z_uz_v$, $G=x_v^2+y_v^2+z_v^2$，那么曲面上由方程 $v=v(u)$ 确定的曲线在\n",
    "$u_1$ 到 $u_2$ 之间的弧长 $s$ 可以表示为积分：\n",
    "$$\n",
    "s=\\int_{u_1}^{u_2}\\sqrt{E+2F\\cdot v'+ G\\cdot\\left(v'\\right)^2}\\mathrm{d}u.\n",
    "$$\n",
    "求这个积分 $s$ 的极小值就是一个泛函极值问题. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.2.3 费马原理(Fermat Principle)\n",
    "    光的空间传播路径耗时最少\n",
    "求光线在两点之间的所经过的路径. 如果介质的折射率是恒定的，那么路径很明显是一条直线. \n",
    "\n",
    "- 但当折射率改变时，或者存在镜子或透镜时，光线的路径就不显然了. \n",
    "法国数学家 费马(Fermat) 在 1657 年提出“光线所走的路径是**时间最短**的”. \n",
    "即，光在任意介质中从一点传播到另一点时，沿所需时间最短的路径传播. \n",
    "又称为“最小时间原理”或“极短光程原理”. \n",
    "\n",
    "[![Fermat%27s_principle](https://en.wikipedia-on-ipfs.org/I/m/Snells_law.svg.png)](https://en.wikipedia-on-ipfs.org/wiki/Fermat%27s_principle.html)\n",
    "(Fermat's principle leads to *Snell's law*; \n",
    "when the sines of the angles in the different media are \n",
    "in the same proportion as the propagation velocities, \n",
    "the time to get from P to Q is minimized.)\n",
    "\n",
    "利用这一原理，就能用变分法求解光的传播路径. \n",
    "先把光线的各种可能路径所耗时间表示为“作用量”泛函，然后找出泛函极值对应的路径就是光线真实的轨迹. \n",
    "\n",
    "---\n",
    "假设 $v$ 是光在介质中传播的速度 $v=\\mathrm{d}s/\n",
    "\\mathrm{d}t$，这里 $s$ 是曲线的弧长，$c$ \n",
    "是光在真空中的传播速度，$n=c/v$ 是折射率. \n",
    "于是光从 $P$ 到 $Q$ 所需的时间\n",
    "$$\n",
    "T=\\int_P^Q \\mathrm{d}t=\\frac1c\\int_P^Q\\frac{c}{v} \n",
    "\\frac{\\mathrm{d}s}{\\mathrm{d}t}\\mathrm{d}t\n",
    "=\\frac1c\\int_P^Q n\\mathrm{~d}s.\n",
    "$$\n",
    "如果定义从 $P$ 到 $Q$ 的曲线集合：\n",
    "$\\Gamma=\\left\\{x(t)\\in\\mathbb{R}^3:\n",
    "x(p)=P, x(q)=Q\\right\\}$，折射率 \n",
    "$n=n(x)$，\n",
    "则变分问题是\n",
    "$$\n",
    "\\inf_{x\\in\\Gamma}\\int_p^q n\\left(x(t)\\right)\n",
    "\\left|\\dot{x}\\right|\\mathrm{d}t.\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.2.4 最速降线问题(Brachistochrone)\n",
    "    质点在重力作用下滑落到不同铅垂线上的另一点耗时最短的路径\n",
    "最早是伽利略在1630年提出了最速降线问题：\n",
    "设A和B是铅垂平面上不在同一铅垂线上的两点，在所有连接A和B的平面曲线中，\n",
    "求出一条曲线，使仅受重力作用且初速度为零的质点从A点到B点沿这条曲线运动时所需**时间最短**. \n",
    "\n",
    "- 问题的提出者伽利略在没有微积分的知识下给出了错误的结果，他认为是圆. \n",
    "- 约翰·伯努利最先得出了正确答案. 他的解法法比较巧妙，将从A点到B点的空间划分出无数层状介质，并把质点的运动和光线传播联系在一起，利用费马原理得到最速降线是摆线这一答案. \n",
    "- ***1696年***，约翰·伯努利向整个数学界发起了挑战，并且声称自己已经解出答案，如果年底没有人解出来，他就会公布答案. \n",
    "1697年1月1日，约翰·伯努利正要公布答案时收到了来自老师莱布尼兹的答案，并在莱布尼兹的要求下延长时间. \n",
    "当时整个欧洲数学界都被此问题吸引，纷纷投入研究. 最后，约翰·伯努利收到了四份答案，分别来自莱布尼茨（约翰·伯努利的老师）、洛必达（约翰·伯努利的学生）、雅各布·伯努利（约翰·伯努利的兄弟）及匿名（牛顿）的答案. \n",
    "- 牛顿、莱布尼兹、洛必达都是凭借深厚的微积分功底解决这个问题. \n",
    "- 1734年，约翰·伯努利的学生 欧拉 开创性地用变分法解决了这个问题. \n",
    "\n",
    "最速降线有许多种证明方法，但对这个问题的研究却产生了一种新的数学——变分法. \n",
    "这是历史上第一个出现的变分法问题，也是变分法发展的一个标志. \n",
    "因为这个看似简单的问题的解并不像前两个问题那么显然，正确答案是一条*摆线*. \n",
    "\n",
    "[![Brachistochrone_curve](https://en.wikipedia-on-ipfs.org/I/m/Brachistochrone.gif)](https://en.wikipedia-on-ipfs.org/wiki/Brachistochrone_curve.html)\n",
    "(The solution to the brachistochrone problem is not a straight line or some combination thereof but a *cycloid*.)\n",
    "\n",
    "类似于费马原理的解法，这里也选取时间作为泛函. \n",
    "通过位移微元除以速度，再对整个路径做一个积分，就得到时间泛函关于路径函数的表达式. 剩下的问题就是变分法了. \n",
    "\n",
    "---\n",
    "为了方便，选取初始位置为坐标原点. \n",
    "由 $\\vec{F} = m\\vec{g} = -\\nabla V$，我们有势能 $V(y)=-mgy$，因此由能量守恒，有\n",
    "$$\\frac12 mv^2=mgy,$$\n",
    "解得 $v=\\sqrt{2gy}$. \n",
    "记平面内的距离微元为 $\\mathrm{d}s=\\sqrt{\\left(\\mathrm{d}x\n",
    "\\right)^2+\\left(\\mathrm{d}y\\right)^2}=\\sqrt{1+{y'}^2}\\mathrm{d}x$，由微分学知识，\n",
    "$v=\\mathrm{d}s/\\mathrm{d}t$，于是\n",
    "$$\n",
    "\\mathrm{d}t=\\frac{\\mathrm{d}s}{v}=\\frac{\\sqrt{1+{y'}^2}\\mathrm{d}x}{\\sqrt{2gy}},\n",
    "$$\n",
    "两边积分，得质点沿一定的光滑路径 $l$ 下滑所需时间为\n",
    "$$\n",
    "T=\\int_0^T\\mathrm{d}t=\\int_l \\frac{\\mathrm{d}s}{v}=\\frac1{\\sqrt{2g}}\\int_{x_1}^{x_2} \\sqrt{\\frac{1+{y'}^2}{y}}\\mathrm{d}x.\n",
    "$$\n",
    "对于每一条选定的光滑曲线 $l$，积分 $T$ 都有一个确定的值与之对应. \n",
    "也就是说，$T$ 是依赖于曲线 $y=y(x)$ 的，不妨记 $T=T[y]$. \n",
    "因此有捷线问题的数学提法：\n",
    "\n",
    "在满足固定端点条件的光滑曲线集合 \n",
    "$\\Gamma=\\left\\{y=y(x)\\in C^1[x_1,x_2]:y(x_1)=y_1, y(x_2)=y_2\\right\\}$\n",
    "中，确定一条曲线使积分 $T[y(\\cdot)]$ 取极小值，即\n",
    "$$\n",
    "\\inf_{y\\in\\Gamma}\\int_{x_1}^{x_2} \\sqrt{\\frac{1+{y'}^2}{y}}\\mathrm{d}x. \n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.2.5 悬链线问题(Catenary)\n",
    "    两端固定的绳子在均匀引力作用下下垂的曲线形状\n",
    "这一问题源于画家 达·芬奇 对于“项链”构图问题的思考：\n",
    "固定项链的两端，使其在重力的作用下自然下垂，那么项链所形成的曲线是什么？\n",
    "惠更斯、伽利略、雅各布·伯努利都对此问题有过尝试，但最终是约翰·伯努利给出了这个问题的正确答案. 悬链线的标准方程是一个双曲余弦函数，而不是抛物线. \n",
    "\n",
    "[![Catenary](https://en.wikipedia-on-ipfs.org/I/m/SpiderCatenary.jpg)](https://en.wikipedia-on-ipfs.org/wiki/Catenary.html)\n",
    "(The silk on a spider's web forming multiple elastic *catenaries*.)\n",
    "\n",
    "- 设绳子两端 $P(x_1,y_1)$, $Q(x_2,y_2)$，其间距小于绳长 $l$. 假设绳子的最低点为 $A(0,y_0)$，取 $y$ 轴过点 $A$，\n",
    "状态方程为 $y=y(x)$，从 $A$ 至任一点 $M(x,y)$ 的弧长为 $s$，则\n",
    "$$\n",
    "s = \\int_0^x \\sqrt{1+\\left(y'\\right)^2}\\mathrm{d}x.\n",
    "$$\n",
    "由于绳索总长度为 $l=\\int_{x_1}^{x_2} \\sqrt{1+\\left(y'\\right)^2}\\mathrm{d}x$ 且平衡时重心最低，\n",
    "因此重心的纵坐标 $\\bar{y}$ 满足\n",
    "$$\n",
    "\\bar{y}=J[y(\\cdot)]=\\frac1l\\int_{x_1}^{x_2} y(x)\\sqrt{1+\\left(y'\\right)^2}\\mathrm{d}x.\n",
    "$$\n",
    "\n",
    "这样就把系统的重心高度作为泛函，将求**最低重心**的问题转化为泛函极值问题，即\n",
    "$$\n",
    "\\inf_{y\\in\\Gamma}\\int_{x_1}^{x_2} y\\sqrt{1+\\left(y'\\right)^2}\\mathrm{d}x. \n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.2.6 极小曲面问题(Minimal Surface)\n",
    "    张在给定的空间闭曲线 Γ 上有最小面积的曲面称为极小曲面\n",
    "【普拉托实验】\n",
    "把围成封闭曲线的金属丝放入肥皂溶液中，然后取出来，由于表面张力的作用，在它上面就蒙有表面积最小的薄膜. \n",
    "这种表面积最小的曲面就是所谓极小曲面，从数学上求这膜曲面的问题称为普拉托问题(Plateau problem). \n",
    "这个问题可以用变分法来解. \n",
    "\n",
    "[![Minimal_surface](https://en.wikipedia-on-ipfs.org/I/m/Bulle_de_savon_h%C3%A9lico%C3%AFde.PNG)](https://en.wikipedia-on-ipfs.org/wiki/Minimal_surface.html)\n",
    "(A *helicoid* minimal surface formed by a soap film on a helical frame)\n",
    "\n",
    "- 从变分学观点看，可以考虑以已知闭曲线 $\\Gamma$ 为固定边界的曲面的*法向变分*. \n",
    "由欧拉-拉格朗日方程，对于任何这样的变分，曲面面积达到临界值的充要条件是*曲面的平均曲率为0*. \n",
    "因此，通常就用这个几何条件来定义极小曲面. \n",
    "\n",
    "---\n",
    "设 $l$ 是 $\\Gamma$ 在平面 $xOy$ 上的投影，$D$ 是 $l$ 所围的平面区域，以 $\\Gamma$ \n",
    "为边界的任一光滑曲面方程为\n",
    "$$\n",
    "z=z(x,y),\\; (x,y)\\in D.\n",
    "$$\n",
    "由积分学，可知曲面面积为\n",
    "$$\n",
    "S[z(\\cdot,\\cdot)]=\\iint_D\\sqrt{1+\\left(\\frac{\\partial z}{\\partial x}\\right)^2+\n",
    "\\left(\\frac{\\partial z}{\\partial y}\\right)^2}\\mathrm{d}x\\mathrm{d}y.\n",
    "$$\n",
    "设 $M$ 为 $l$ 上任一点，在 $\\Gamma$ 上与 $M$ 对应的点的纵坐标为已知函数 $g(M)$，即\n",
    "$z|_l=g(M),\\;M\\in l$. 这样就把极小曲面问题归结为给定边界条件下在光滑曲面集合中求有**最小面积**的曲面. \n",
    "即泛函极值问题. \n",
    "\n",
    "更一般地，对于非参数化曲面 $\\Sigma=\\left\\{\n",
    "v(x)=(x,u(x))\\in\\mathbb{R}^{2+1}:x\\in\\Omega\n",
    "\\right\\}$，这里 $u:\\bar{\\Omega}\\to\\mathbb{R}$，\n",
    "$\\Omega\\subset\\mathbb{R}^2$ 是有界区域. \n",
    "这些曲面都满足边界条件：$\\partial\\Sigma=\\Gamma$.\n",
    "于是设 $W=\\left\\{u\\in C^1\\left(\\bar{\\Omega}\\right):\n",
    "u=u_0(x),x\\in\\partial\\Omega\\right\\}$，则问题成为\n",
    "$$\n",
    "\\inf_{u\\in W}\\int_\\Omega \\sqrt{1+\n",
    "\\left|\\nabla u\\right|^2}\\mathrm{d}x.\n",
    "$$\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.2.7 最小旋转面问题(Minimal Surface of Revolution)\n",
    "    极小曲面的一个特例\n",
    "类似于捷线问题的设定，仍然是在不同铅垂直线上的两点 $A(x_1,y_1)$，$B(x_2,y_2)$. \n",
    "在连接 $AB$ 两点的所有平面曲线中，求一条使它围绕 $Ox$ 轴旋转使所得的曲面具有最小的面积. \n",
    "\n",
    "[![Minimal_surface_of_revolution](https://en.wikipedia-on-ipfs.org/I/m/Bulle_cat%C3%A9no%C3%AFde.png)](https://en.wikipedia-on-ipfs.org/wiki/Minimal_surface_of_revolution.html)\n",
    "(Stretching a soap film between two parallel circular wire loops generates a *catenoidal* minimal surface of revolution)\n",
    "\n",
    "旋转面的面积为旋转体的侧面积，即曲线弧长微元乘以旋转圆周长，然后对整个曲线积分. \n",
    "$$\n",
    "S[y] = \\int 2\\pi y\\mathrm{~d}s = \n",
    "2\\pi \\int_{x_1}^{x_2} y\\sqrt{1+{y'}^2}\\mathrm{d}x\n",
    "$$\n",
    "其中，$y' = \\mathrm{d}y/\\mathrm{d}x$. \n",
    "注意到，这里 $L = L(y,y') = y\\sqrt{1+{y'}^2}$ 与自变量 $x$ 无关. \n",
    "\n",
    "值得注意的是，虽然前面的系数不同，但我们可以看到这里的最小旋转面问题和悬链线问题所要求极值的泛函是一样的，即变分问题\n",
    "$$\n",
    "\\inf_{y\\in\\Gamma}\\int_{x_1}^{x_2} y\\sqrt{1+\\left(y'\\right)^2}\\mathrm{d}x.\n",
    "$$\n",
    "因此这两个问题在适当的条件下能得到同样的结果也就不奇怪了，因为其本质上是同一个变分问题. \n",
    "\n",
    "---\n",
    "但这不一定总是可行的，因为我们求出来的解都是二阶可微的. \n",
    "由于两端点相距足够远时曲面极小解会不连续，所以该问题的通解被称为 **Goldschmidt 不连续解**. \n",
    "\n",
    "相对应的悬链线也要求两端点距离不能大于绳长，不然这个模型就行不通了. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.2.8 小结\n",
    "通过前面几个简单的例子，我们看到确实有不少\n",
    "\n",
    "- **物理规律/自然现象**，如“费马原理、悬链线、极小曲面”，\n",
    "- **工程设计/最优控制**，如“测地线、最速降线、等周问题”，\n",
    "\n",
    "问题的本质是对一个泛函求最值. \n",
    "而在实际应用中的最值往往是在某一极值点处取到，所以研究泛函极值问题，即引入“变分法”是很有必要的. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.3 极值的必要条件——欧拉方程\n",
    "处理泛函极值的变分法的核心就是，泛函取极值的必要条件是**一阶变分等于零**. \n",
    "这虽然是必要条件，但在大多数物理背景的问题中一阶变分等于零往往能得到泛函的极值曲线(extremal function/curve). \n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.3.1 变分的概念和记号\n",
    "类比函数微分的定义，我们可以定义泛函变分. 变分类似于微分，也是一个微小的改变. 两种算符都可以作用在函数上. \n",
    "而不同的是，变分是在自变量不变的情况下，函数的小变化对应的**泛函增量（变化量）的线性部分**；\n",
    "微分则是自变量的一个小变化对应的**函数增量的线性部分**. \n",
    "\n",
    "**定义**【函数的变分】\n",
    "泛函 $\\mathcal{A}[x(t)]$ 的自变量 $x(t)$ 的变分 $\\delta x$ 是指容许函数类中的两个函数 $x_1(t)$ 与 $x_2(t)$ \n",
    "之差：\n",
    "$$\n",
    "\\delta x = x_1(t)-x_2(t)\n",
    "$$\n",
    "这里 $t$ 是参数，或者说，在变分运算中可以认为 $t$ 是固定不变的（但可以在容许范围内取任意值）. \n",
    "\n",
    "如果容许函数类由可微函数组成，则有\n",
    "$\n",
    "\\mathrm{d}(\\delta x) = \\mathrm{d}[x_1(t)-x_2(t)]\n",
    "= \\mathrm{d}[x_1(t)] - \\mathrm{d}[x_2(t)]\n",
    "= \\delta(\\mathrm{d}x).\n",
    "$\n",
    "也就是说，此时函数的微分运算和变分运算的次序是可以交换的. \n",
    "\n",
    "**定义**【泛函的变分】\n",
    "如果泛函 $\\mathcal{A}[x(t)]$ 的改变量\n",
    "$\n",
    "\\Delta \\mathcal{A} = \\mathcal{A}[x(t)+\\delta x] - \\mathcal{A}[x(t)]\n",
    "$\n",
    "可以表示为如下形式：\n",
    "$$\n",
    "\\Delta \\mathcal{A} = T[x(t),\\delta x] + \\beta(x(t),\\delta x)\\max|\\delta x|,\n",
    "$$\n",
    "其中 $T[x(t),\\delta x]$ 对于 $\\delta x$ 来说是线性的，且当 $\\max|\\delta x|\\to0$ 时有 \n",
    "$\\beta(x(t),\\delta x)\\to0$，则称 $T[x(t),\\delta x]$ 为泛函 $\\mathcal{A}[x(t)]$ 的变分，记为 $\\delta \\mathcal{A}$. \n",
    "\n",
    "也就是说，泛函变分是**函数变分所引起泛函增量的线性部分**. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "作为无穷小分析的变分算符 $\\delta$ 和微分算符 $d$ 具有相似的性质，\n",
    "而且变分可以与积分和微分（求导）符号**交换顺序**. 下面给出几个运算法则：\n",
    "1. $\\delta\\left(\\mathcal{A}_1+\\mathcal{A}_2\\right) = \\delta \\mathcal{A}_1 + \\delta \\mathcal{A}_2$；\n",
    "1. $\\delta\\left(\\mathcal{A}_1\\cdot \\mathcal{A}_2\\right) = \\mathcal{A}_1\\delta \\mathcal{A}_2 + \\mathcal{A}_2\\delta \\mathcal{A}_1$；\n",
    "1. $\\delta\\left(\\mathcal{A}_1 / \\mathcal{A}_2\\right) = \\left(\\mathcal{A}_2\\delta \\mathcal{A}_1-\\mathcal{A}_1\\delta \\mathcal{A}_2\\right)/(\\mathcal{A}_2^2)$；\n",
    "1. $\\delta\\left(\\mathcal{A}^n\\right) = n\\mathcal{A}^{n-1}\\delta \\mathcal{A}$；\n",
    "1. $(\\delta x)^{(n)}(t) = \\delta x^{(n)}(t)$，即函数变分的导数等于函数导数的变分.\n",
    "\n",
    "---\n",
    "变分的容许曲线可以表示为极值曲线加上一个函数变分 $\\delta x = \\alpha\\eta(t)$，\n",
    "$$x(t)=x^*(t)+\\alpha\\eta(t),$$\n",
    "其中的极值曲线也有记作相应大写字母的，如 $X(t)$. \n",
    "\n",
    "通过引入参数 $\\alpha$，我们可以把原来的泛函变分转化成函数微分，即 $\\delta\\mathcal{A}|_{x=x^*(\\cdot)} = \\alpha~\\Phi'(0)$. \n",
    "其中 \n",
    "$$\\Phi(\\alpha)=\\mathcal{A}[x(\\cdot)]=\n",
    "\\mathcal{A}[x^*(\\cdot)+\\alpha\\eta(\\cdot)].\n",
    "$$\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.3.2 一个简单泛函的变分导数\n",
    "令 $x$ 为平面内的一条曲线，且可微函数 $L=L\\left(x(t),\\dot{x}(t),t\\right)$. 那么泛函 \n",
    "$$\n",
    "\\mathcal{A}[x] = \\int_{t_0}^{t_1} L\\left(x(t),\\dot{x}(t),t\\right)\\mathrm{d}t\n",
    "$$\n",
    "是“可微的”，并且其变分导数为 \n",
    "$$\n",
    "T(h) = \\int_{t_0}^{t_1}\\left[\\frac{\\partial L}{\\partial x}-\\frac{\\mathrm{d}}{\\mathrm{d}t}\\left(\\frac{\\partial L}{\\partial \\dot{x}}\\right)\\right]h\\mathrm{d}t + \\left.\\frac{\\partial L}{\\partial \\dot{x}} h\\right|_{t_0}^{t_1}\n",
    "$$\n",
    "**证明**\n",
    "因为 $f$ 是可微函数，所以\n",
    "$$\\begin{aligned}\n",
    "\\mathcal{A}[x+h] - \\mathcal{A}[x] \n",
    "&= \\int_{t_0}^{t_1} \\left[L\\left(x+h,\\dot{x}+\\dot{h},t\\right) - L\\left(x,\\dot{x},t\\right)\\right]\\mathrm{d}t\\\\\n",
    "&= \\int_{t_0}^{t_1} \\left(\\frac{\\partial L}{\\partial x}h + \\frac{\\partial L}{\\partial \\dot{x}}\\dot{h}\\right)\\mathrm{d}t + \\mathcal{O}(h^2)\\\\\n",
    "&:= T(h)+R(h,x)\n",
    "\\end{aligned}\n",
    "$$\n",
    "其中，我们定义了 $T(h) = \\int_{t_0}^{t_1} \\left[\\left(\\partial L/\\partial x\\right)h + \\left(\\partial L/\\partial \\dot{x}\\right)\\dot{h}\\right]\\mathrm{d}t$ 和 $R(h,x) = \\mathcal{O}(h^2)$. 由定义，泛函的变分导数存在就意味着它是可微的. 对 $T(h)$ 的第二项做分部积分，我们有\n",
    "$$\n",
    "\\int_{t_0}^{t_1} \\frac{\\partial L}{\\partial \\dot{x}} \\frac{\\mathrm{d}h}{\\mathrm{d}t} \\mathrm{d}t = \\left.\\frac{\\partial L}{\\partial \\dot{x}}h\\right|_{t_0}^{t_1} - \\int_{t_0}^{t_1} \\frac{\\mathrm{d}}{\\mathrm{d}t}\\left(\\frac{\\partial L}{\\partial \\dot{x}}\\right)h\\mathrm{d}t\n",
    "$$\n",
    "代回 $T(h)$，结论命题得证. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-02-02T16:09:18+08:00",
     "start_time": "2021-02-02T08:09:18.494Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><img alt=\"Output\" src=\"\"></div>"
      ],
      "text/plain": [
       "FormBox[RowBox[{RowBox[{FractionBox[RowBox[{\"∂\", RowBox[{\"η\", \"(\", \"t\", \")\"}]}], \n",
       "\n",
       " \n",
       ">           RowBox[{\"∂\", \"t\"}], Rule[MultilineFunction, None]], \" \", \n",
       "\n",
       " \n",
       ">          FractionBox[RowBox[{\"∂\", RowBox[{\"L\", \"(\", \n",
       "\n",
       " \n",
       ">               RowBox[{RowBox[{SuperscriptBox[\"x\", \"*\"], \"(\", \"t\", \")\"}], \",\", \n",
       "\n",
       " \n",
       ">                 RowBox[{SuperscriptBox[RowBox[{\"(\", SuperscriptBox[\"x\", \"*\"], \n",
       "\n",
       " \n",
       ">                      \")\"}], \"′\", Rule[MultilineFunction, None]], \"\", \n",
       "\n",
       " \n",
       ">                   RowBox[{\"(\", \"t\", \")\"}]}], \",\", \"t\"}], \")\"}]}], \n",
       "\n",
       " \n",
       ">           RowBox[{\"∂\", RowBox[{SuperscriptBox[RowBox[{\"(\", SuperscriptBox[\"x\", \"*\"], \n",
       "\n",
       " \n",
       ">                  \")\"}], \"′\", Rule[MultilineFunction, None]], \"\", \n",
       "\n",
       " \n",
       ">               RowBox[{\"(\", \"t\", \")\"}]}]}], Rule[MultilineFunction, None]]}], \"+\", \n",
       "\n",
       " \n",
       ">        RowBox[{RowBox[{\"η\", \"(\", \"t\", \")\"}], \" \", \n",
       "\n",
       " \n",
       ">          FractionBox[RowBox[{\"∂\", RowBox[{\"L\", \"(\", \n",
       "\n",
       " \n",
       ">               RowBox[{RowBox[{SuperscriptBox[\"x\", \"*\"], \"(\", \"t\", \")\"}], \",\", \n",
       "\n",
       " \n",
       ">                 RowBox[{SuperscriptBox[RowBox[{\"(\", SuperscriptBox[\"x\", \"*\"], \")\"}], \n",
       "\n",
       " \n",
       ">                    \"′\", Rule[MultilineFunction, None]], \"\", RowBox[{\"(\", \"t\",\\\n",
       " \n",
       ">    \")\"}]}]\\\n",
       ", \",\", \"t\"}], \")\"}]}], RowBox[{\"∂\", \n",
       "\n",
       " \n",
       ">             RowBox[{SuperscriptBox[\"x\", \"*\"], \"(\", \"t\", \")\"}]}], \n",
       "\n",
       " \n",
       ">           Rule[MultilineFunction, None]]}]}], TraditionalForm]\n"
      ]
     },
     "execution_count": 54,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(*最简变分问题*)\n",
    "Block[\n",
    "{f, L, x, xdot, t, e, \\[Eta]},\n",
    "x[t, e] := SuperStar[x][t] + e*\\[Eta][t];\n",
    "xdot[t_,e_] := D[x[t,e], t];\n",
    "f = L[x[t,e], xdot[t,e], t];\n",
    "D[f, e]/.e->0//pdConv\n",
    "]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "记 $x(t)=x^*(t)+\\alpha\\cdot\\eta(t)$，则 $h(t)=\\alpha\\cdot\\eta(t)$. 相应地，有\n",
    "$$\n",
    "\\lim_{\\alpha\\to0}\\frac{\\mathcal{A}[x+h] - \\mathcal{A}[x]}{\\alpha}=\\eta(t)\\frac{\\delta \\mathcal{A}}{\\delta x}\n",
    "$$\n",
    "\n",
    "忽略计算结果中的函数 $L$ 所依赖的变量，有\n",
    "$$\n",
    "\\frac{\\partial L}{\\partial \\dot{x}}\\dot{\\eta} + \\frac{\\partial L}{\\partial x}\\eta =\n",
    "\\frac{\\partial L}{\\partial x}\\eta + \\frac{\\partial L}{\\partial \\dot{x}} \n",
    "\\frac{\\mathrm{d}\\eta}{\\mathrm{d}t}\n",
    "$$\n",
    "再把外层的积分号补上，分部积分得到\n",
    "\n",
    "$$\n",
    "\\begin{aligned}\n",
    "&\\int_{t_0}^{t_1}\\frac{\\partial L}{\\partial x}\\eta+\\frac{\\mathrm{d}}\n",
    "{\\mathrm{d}t}\\left(\\frac{\\partial L}{\\partial \\dot{x}} \\eta\\right)-\\frac{\\mathrm{d}}\n",
    "{\\mathrm{d}t}\\left(\\frac{\\partial L}{\\partial \\dot{x}}\\right)\\eta\\mathrm{d}t\\\\\n",
    " = \\;&\\boxed{\n",
    "\\int_{t_0}^{t_1}\\left[\\frac{\\partial L}{\\partial x}-\\frac{\\mathrm{d}}\n",
    "{\\mathrm{d}t}\\left(\\frac{\\partial L}{\\partial \\dot{x}}\\right)\\right]\\eta \\mathrm{d}t + \n",
    "\\left.\\frac{\\partial L}{\\partial \\dot{x}} \\eta\\right|_{t_0}^{t_1}}\n",
    "\\end{aligned}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.3.3 泛函极值存在的必要条件\n",
    "下面我们以一个简单泛函 $\\mathcal{A}[x] = \\int_{t_0}^{t_1} L\\left(x(t),\\dot{x}(t),t\\right)\\mathrm{d}t$ 为例讨论泛函极值存在的必要条件. \n",
    "\n",
    "设泛函 $\\mathcal{A}[x] = \\int_{t_0}^{t_1} L\\left(x(t),\\dot{x}(t),t\\right)\\mathrm{d}t, L\\in C^2$，\n",
    "其容许函数类为 \n",
    "$$\n",
    "X = \\left\\{x| x\\in C^1[t_0,t_1], x(t_0)=x_0, x(t_1)=x_1\\right\\}. \n",
    "$$\n",
    "利用二阶泰勒公式，有\n",
    "$$\\begin{aligned}\n",
    "\\Delta \\mathcal{A} \n",
    "= &\\mathcal{A}[x+\\delta x] - \\mathcal{A}[x]\\\\\n",
    "= &\\int_{t_0}^{t_1} \\left[L\\left(x+\\delta x,\\dot{x}+\\delta\\dot{x},t\\right) - \n",
    "L\\left(x,\\dot{x},t\\right)\\right]\\mathrm{d}t\\\\\n",
    "= &\\int_{t_0}^{t_1} \\left[L_{x}\\left(x,\\dot{x},t\\right)\\delta x + \n",
    "L_{\\dot{x}}\\left(x,\\dot{x},t\\right)\\right]\\delta \\dot{x}\\mathrm{d}t + \\\\\n",
    "\\frac12 &\\int_{t_0}^{t_1} L_{xx}\\left(x+\\theta\\delta x,\\dot{x}+\\theta\\delta\\dot{x},t\\right) \n",
    "\\left(\\delta x\\right)^2\\mathrm{d}t + \\\\\n",
    "&\\int_{t_0}^{t_1} L_{x\\dot{x}}\\left(x+\\theta\\delta x,\\dot{x}+\\theta\\delta\\dot{x},t\\right) \n",
    "\\delta x\\delta\\dot{x}\\mathrm{d}t + \\\\\n",
    "\\frac12 &\\int_{t_0}^{t_1} L_{\\dot{x}\\dot{x}}\\left(x+\\theta\\delta x,\\dot{x}+\n",
    "\\theta\\delta\\dot{x},t\\right)\\left(\\delta\\dot{x}\\right)^2 \\mathrm{d}t \\\\\n",
    "\\triangleq &\\int_{t_0}^{t_1} \\left(L_{x}\\delta x + L_{\\dot{x}}\\delta\\dot{x}\\right) \n",
    "\\mathrm{d}t + \\beta(t, x, \\delta x, \\theta), \\; 0<\\theta<1. \n",
    "\\end{aligned}\n",
    "$$\n",
    "因为 $L\\in C^2$，所以当 $d_1(x, x+\\delta x)$ 充分小时，有\n",
    "$$\n",
    "\\begin{aligned}\n",
    "L_{xx}\\left(x+\\theta\\delta x,\\dot{x}+\\theta\\delta\\dot{x},t\\right) &\\le M\\\\\n",
    "L_{x\\dot{x}}\\left(x+\\theta\\delta x,\\dot{x}+\\theta\\delta\\dot{x},t\\right) &\\le M\\\\\n",
    "L_{\\dot{x}\\dot{x}}\\left(x+\\theta\\delta x,\\dot{x}+\\theta\\delta\\dot{x},t\\right) &\\le M\n",
    "\\end{aligned}\n",
    "$$\n",
    "故 $\\lim_{d_1\\to0}\\beta/d_1=0$，即变分的高阶项可忽略. \n",
    "而 $\\int_{t_0}^{t_1} \\left(L_{x}\\delta x + L_{\\dot{x}}\\delta\\dot{x}\\right) \\mathrm{d}t$ 是关于 \n",
    "$\\delta x$ 的线性泛函（类比线性函数），故一阶变分\n",
    "$$\n",
    "\\delta \\mathcal{A}[x] = \\int_{t_0}^{t_1} \\left(L_{x}\\delta x + L_{\\dot{x}}\\delta\\dot{x}\\right) \\mathrm{d}t.\n",
    "$$\n",
    "若记 $\\delta L = L_{x}\\delta x + L_{\\dot{x}}\\delta\\dot{x}$，则有\n",
    "$$\n",
    "\\delta \\mathcal{A} = \\int_{t_0}^{t_1} \\delta L \\mathrm{d}t.\n",
    "$$\n",
    "称 $\\delta L$ 为函数 $L$ 的变分. \n",
    "\n",
    "设 $\\mathcal{A}[x]$ 为线性赋范空间 $X$ 上的泛函，设 $x^{*}$ 及 $x^{*}+\\alpha\\eta\\in X$，其中 $\\eta(x)\\in X$，\n",
    "$\\alpha$ 为任意常数. 记 $\\Phi(\\alpha) = \\mathcal{A}[x^{*}+\\alpha\\eta]$. \n",
    "若 $\\mathcal{A}[x]$ 在 $x^{*}$ 处取得极值（即$T=0$）且在 $x^{*}$ 处泛函变分存在，则有\n",
    "$$\n",
    "\\Phi(\\alpha)-\\Phi(0) = \\mathcal{A}[x^{*}+\\alpha\\delta\\eta]-\\mathcal{A}[x^{*}] \n",
    "= \\alpha T[x^{*},\\delta\\eta]+\\beta(x^{*}, \\alpha\\delta\\eta),\n",
    "$$\n",
    "于是\n",
    "$$\n",
    "\\frac{\\Phi(\\alpha)-\\Phi(0)}{\\alpha-0} \n",
    "= T[x^{*},\\delta\\eta]+\\frac{\\beta(x^{*}, \\alpha\\delta\\eta)}{\\alpha},\n",
    "$$\n",
    "因此有\n",
    "$$\n",
    "\\left.\\frac{\\partial}{\\partial\\alpha}\\mathcal{A}[x^{*}+\\alpha\\eta]\\right|_{\\alpha=0} = \n",
    "\\Phi'(0) = \\lim_{\\alpha\\to0} \\frac{\\Phi(\\alpha)-\\Phi(0)}{\\alpha-0} = 0\n",
    "$$\n",
    "故 $\\delta \\mathcal{A}[x^{*}] = 0$. \n",
    "\n",
    "---\n",
    "变分法的基本方程是欧拉方程\n",
    "$$\n",
    "\\frac{\\partial L}{\\partial x} = \\frac{\\mathrm{d}}{\\mathrm{d}t}\\left(\\frac{\\partial L}{\\partial \\dot{x}}\\right)\n",
    "$$\n",
    "从泛函极值存在的必要条件，即“泛函的一阶变分等于零”，就能推导出该方程. 但在此之前，我们还需要一个引理. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.3.4 变分基本引理\n",
    "**引理 1**【最简】(The DuBois-Reymond Lemma, published in 1879)\n",
    "\n",
    "设函数 $f(x)\\in C[a,b]$ 对任意函数 $\\eta(x)\\in C_0[a,b]$\n",
    "（i.e. $\\eta(x)$ 在 $[a,b]$ 上连续，且满足 $\\eta(a)=\\eta(b)=0$），\n",
    "若积分 $\\int_a^b f(x)\\eta(x)\\mathrm{d}x = 0$ 总成立，则在区间 $[a,b]$ 上必有 $f\\equiv0$. \n",
    "\n",
    "**证明**\n",
    "取 $\\eta(x)=f(x)(x-a)(b-x)$，则 $\\eta(x)$ 在 $[a,b]$ 上连续，且满足 $\\eta(a)=\\eta(b)=0$. \n",
    "根据假设，有\n",
    "$$\n",
    "0=\\int_a^b f(x)\\eta(x)\\mathrm{d}x=\\int_a^b f^2(x)(x-a)(b-x)\\mathrm{d}x, \n",
    "$$\n",
    "而 $f(x)\\eta(x)\\ge0$，故有 $f(x)\\eta(x)\\equiv0$，因此 $f\\equiv0$. \n",
    "\n",
    "---\n",
    "一般地，我们还有：\n",
    "\n",
    "**引理 2**【高阶】\n",
    "设函数 $f(x)\\in C[a,b]$ 对任意函数 $\\eta(x)$ 在区间 $[a,b]$ 上具有 $n$ 阶导数，且对于某个正数 $m$（$m=0,1,2,\\cdots,n$）满足条件\n",
    "$$\n",
    "\\eta^{(k)}(a) = \\eta^{(k)}(b) = 0, \\; k=0,1,2,\\cdots,m. \n",
    "$$\n",
    "若积分 $\\int_a^b f(x)\\eta(x)\\mathrm{d}x = 0$ 总成立，则在区间 $[a,b]$ 上必有 $f\\equiv0$. \n",
    "\n",
    "**证明**\n",
    "用反证法：若 $f(x)\\not\\equiv0$，则存在 $\\xi\\in(a,b)$，使得 $f(\\xi)\\ne0$，不妨设 $f(\\xi)>0$，\n",
    "则必存在 $[a_0,b_0]$，使 $a<a_0\\le\\xi\\le b_0<b$ 和 $f(x)>0$. 取函数\n",
    "$$\n",
    "\\eta(x)=\\left\\{\n",
    "\\begin{matrix}\n",
    "\\left[(x-a_0)(b_0-x)\\right]^{2n+2}, & x\\in[a_0,b_0],\\\\\n",
    "0, & x\\not\\in[a_0,b_0].\n",
    "\\end{matrix}\n",
    "\\right.\n",
    "$$\n",
    "那么 $\\eta^{(k)}(a) = \\eta^{(k)}(b) = 0, \\; k=0,1,2,\\cdots,m$，故\n",
    "$$\n",
    "\\int_a^b f(x)\\eta(x)\\mathrm{d}x = \\int_{a_0}^{b_0} f(x)\\eta(x)\\mathrm{d}x > 0\n",
    "$$\n",
    "与条件矛盾. \n",
    "\n",
    "---\n",
    "**引理 3**【两个函数】\n",
    "若 $f(x), g(x)\\in C[a,b]$，对于任意 $\\xi(x), \\eta(x) \\in C_0[a,b]$，有\n",
    "$$\n",
    "\\int_a^b \\left[f(x)\\xi(x) + g(x)\\eta(x)\\right]\\mathrm{d}x = 0,\n",
    "$$\n",
    "则在区间 $[a,b]$ 上必有 $f(x)\\equiv0, g(x)\\equiv0$. \n",
    "\n",
    "---\n",
    "**引理 4**【二元函数】\n",
    "设函数 $f(x,y)$ 在闭区域 $D$ 上连续，任意函数 $\\eta(x,y)$ 在 $D$ 上连续，且在 $D$ 边界 $\\partial D$ 上 $\\eta(x,y)=0$ 时，若积分 \n",
    "$$\n",
    "\\iint_D f(x,y)\\eta(x,y)\\mathrm{d}x\\mathrm{d}y = 0\n",
    "$$\n",
    "总成立，则在 $D$ 上必有 $f(x,y)\\equiv0$. \n",
    "\n",
    "**证明**\n",
    "用反证法：若 $f(x,y)\\not\\equiv0$，则存在 $A(\\xi,\\zeta)\\in D\\backslash \\partial D$，有\n",
    "$f(\\xi,\\zeta)\\ne0$，不妨设 $f(\\xi,\\zeta)>0$，则必存在 $\\rho>0, \\epsilon>0$，使得\n",
    "$B_\\rho(A)\\subset D$，而 $f(x,y)>\\epsilon$，$\\forall(x,y)\\in B_\\rho(A)$，取\n",
    "$$\n",
    "\\eta(x,y)=\\left\\{\n",
    "\\begin{matrix}\n",
    "\\rho^2 - (x-\\xi)^2 - (y-\\zeta)^2, & (x,y)\\in B_\\rho(A)\\backslash \\partial B_\\rho(A),\\\\\n",
    "0, & (x,y)\\not\\in B_\\rho(A)\\backslash \\partial B_\\rho(A).\n",
    "\\end{matrix}\n",
    "\\right.\n",
    "$$\n",
    "则\n",
    "$$\n",
    "\\begin{aligned}\n",
    "\\iint_D f(x,y)\\eta(x,y)\\mathrm{d}x\\mathrm{d}y \n",
    "&= \\iint_{B_\\rho(A)} f(x,y)\\eta(x,y)\\mathrm{d}x\\mathrm{d}y \\\\\n",
    "&\\ge \\frac12 \\pi\\epsilon\\rho^4 >0\n",
    "\\end{aligned}\n",
    "$$\n",
    "与条件矛盾. \n",
    "\n",
    "---\n",
    "**引理 5**【-】\n",
    "设 $f(x)\\in C^1[a,b]$，且 $f(a)=0$（或 $f(b)=0$），则有\n",
    "$$\n",
    "\\int_a^b \\left(f(x)\\right)^2\\mathrm{d}x\n",
    "\\le\\frac12 (b-a)^2\\int_a^b \\left(f'(x)\\right)^2\\mathrm{d}x\n",
    "$$\n",
    "\n",
    "**证明**\n",
    "由于 $f(a)=0$，故 $f(x)=\\int_a^x f'(t)\\mathrm{d}t$，$\\forall x\\in[a,b]$. 利用 Schwarz 不等式\n",
    "$$\n",
    "\\left(\\int fg\\right)^2 \\le \\int f^2\\cdot \\int g^2, \n",
    "$$\n",
    "则有\n",
    "$$\n",
    "\\begin{aligned}\n",
    "\\int_a^b \\left(f(x)\\right)^2\\mathrm{d}x\n",
    "&= \\int_a^b \\left[\\int_a^x 1\\cdot f'(t)\\mathrm{d}t\\right]^2\\mathrm{d}x \\\\\n",
    "&\\le \\int_a^b \\left[\\int_a^x 1^2 \\mathrm{d}t\\cdot \\int_a^x \\left(f'(t)\\right)^2 \\mathrm{d}t\\right]\\mathrm{d}x \\\\\n",
    "&= \\int_a^b \\left[(x-a)\\cdot \\int_a^x \\left(f'(t)\\right)^2 \\mathrm{d}t\\right]\\mathrm{d}x \\\\\n",
    "&= \\frac12 (b-a)^2\\int_a^b \\left(f'(x)\\right)^2\\mathrm{d}x\n",
    "\\end{aligned}\n",
    "$$\n",
    "对于 $f(b)=0$ 情形，证明类似. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.3.5 固定边界的最简变分问题\n",
    "下面再回过来，系统地研究一下变分问题. 但是直接求解泛函极值问题是非常困难的. \n",
    "欧拉首先发现：在一定条件下，一个泛函的极值对应一个微分方程，即“**使泛函取极值的函数应满足欧拉方程**”. \n",
    "\n",
    "**定理**【Euler 方程】\n",
    "经过 $x_0 = x(t_0), x_1 = x(t_1)$ 这两个点的函数 $y: x=x(t)$ 所构成的函数空间上的一个可微泛函 $\\mathcal{A}[y] = \\int_{t_0}^{t_1} L\\left(x(t),\\dot{x}(t),t\\right)\\mathrm{d}t$ 一阶变分等于0的**充要条件**是：\n",
    "函数 $y$ 满足欧拉(Euler)方程，即\n",
    "$$\n",
    "\\frac{\\mathrm{d}}{\\mathrm{d}t}\\left(\\frac{\\partial L}{\\partial \\dot{x}}\\right) - \n",
    "\\frac{\\partial L}{\\partial x} = 0. \n",
    "$$\n",
    "\n",
    "**证明**\n",
    "由前面一个简单泛函的变分导数的结果，我们有\n",
    "$$\n",
    "T(h) = \\int_{t_0}^{t_1}\\left[\\frac{\\partial L}{\\partial x}-\\frac{\\mathrm{d}}{\\mathrm{d}x}\\left(\\frac{\\partial L}{\\partial \\dot{x}}\\right)\\right]h\\mathrm{d}t + \\left.\\frac{\\partial L}{\\partial \\dot{x}} h\\right|_{t_0}^{t_1}. \n",
    "$$\n",
    "而假设条件“函数 $y$ 经过 $x_0 = x(t_0), x_1 = x(t_1)$ 这两个点”就意味着 $h(t_0) = h(t_1) = 0$，\n",
    "因此上式右手边第二项“边界项”会等于0. \n",
    "又因为一阶变分导数 $T(h)=0$. 即，\n",
    "$$\n",
    "T(h) = \\int_{t_0}^{t_1}\\left[\\frac{\\partial L}{\\partial x}-\\frac{\\mathrm{d}}{\\mathrm{d}x}\\left(\\frac{\\partial L}{\\partial \\dot{x}}\\right)\\right]h\\mathrm{d}t = 0\n",
    "$$\n",
    "再利用变分基本引理，对于所有 $h$ 我们有欧拉方程\n",
    "$$\n",
    "\\frac{\\partial L}{\\partial x}-\\frac{\\mathrm{d}}{\\mathrm{d}x}\\left(\\frac{\\partial L}{\\partial \\dot{x}}\\right) = 0\n",
    "$$\n",
    "反过来，如果有欧拉方程，那么很明显 $T(h)=0$，因此 $y$ 就是相应地使泛函一阶变分等于0的函数了. \n",
    "\n",
    "---\n",
    "通过把全微分展开，欧拉方程还可以写成\n",
    "$$\n",
    "\\frac{\\partial L}{\\partial x} - \n",
    "\\frac{\\partial^2 L}{\\partial t\\partial \\dot{x}} - \n",
    "\\dot{x}\\frac{\\partial^2 L}{\\partial x\\partial \\dot{x}} - \n",
    "\\ddot{x}\\frac{\\partial^2 L}{\\partial \\dot{x}^2} = 0\n",
    "$$\n",
    "若 $\\partial^2 L/ \\partial \\dot{x}^2\\ne0$，则欧拉方程是一个二阶常微分方程；\n",
    "若欧拉方程有解 $y$，则称 $y$ 为原变分问题的正则点(regular point)，\n",
    "也称为是欧拉方程的**逗留函数**(Stationary function). \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-02-03T15:55:20+08:00",
     "start_time": "2021-02-03T07:55:20.369Z"
    }
   },
   "outputs": [],
   "source": [
    "Options[EulerEqn0] = {eXpand -> False};\n",
    "(* Euler equation for a single variable *)\n",
    "\n",
    "EulerEqn0[dichte_, depend_, independ_, options___] := \n",
    "  Module[{f0, rule, fh, e, w, y, expand}, \n",
    "  (*--- 检查选项 ---*)\n",
    "   {expand} = {eXpand} /. {options} /. Options[EulerEqn0]; \n",
    "   (*--- 定义函数变分 ---*)\n",
    "    f0 = Function[x, y[x] + e*w[x]]; \n",
    "    (*--- 定义替换规则---*)\n",
    "    rule = (b_.)*Derivative[n_][w][independ] :> \n",
    "      (-1)^n*HoldForm[D[b, {independ, n}]]; \n",
    "    (*--- 对因变量的变分 ---*)\n",
    "    fh = dichte /. depend -> f0 /. {x -> independ, y -> depend}; \n",
    "    (*--- 对参数求导，置零 ---*)\n",
    "    fh = Expand[D[fh, e] /. e -> 0]; \n",
    "    (*--- 变换到 w ---*)\n",
    "    fh = fh /. rule /. w[independ] -> 1;\n",
    "    (*--- 欧拉方程 ---*)\n",
    "    If[expand, fh = ReleaseHold[fh==0], fh==0]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-02-02T16:16:10+08:00",
     "start_time": "2021-02-02T08:16:09.929Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><img alt=\"Output\" src=\"\"></div>"
      ],
      "text/plain": [
       "FormBox[RowBox[{RowBox[{RowBox[{\"-\", \n",
       "\n",
       " \n",
       ">            FractionBox[RowBox[{SuperscriptBox[\"∂\", \"2\"], \n",
       "\n",
       " \n",
       ">               RowBox[{\"L\", \"(\", RowBox[{\"t\", \",\", RowBox[{\"x\", \"(\", \"t\", \")\"}], \",\", \n",
       "\n",
       " \n",
       ">                   RowBox[{SuperscriptBox[\"x\", \"′\", Rule[MultilineFunction, None]], \n",
       "\n",
       " \n",
       ">                     \"(\", \"t\", \")\"}]}], \")\"}]}], \n",
       "\n",
       " \n",
       ">             RowBox[{RowBox[{\"∂\", \"t\"}], \" \", \n",
       "\n",
       " \n",
       ">               RowBox[{\"∂\", RowBox[{SuperscriptBox[\"x\", \"′\", \n",
       "\n",
       " \n",
       ">                    Rule[MultilineFunction, None]], \"(\", \"t\", \")\"}]}]}], \n",
       "\n",
       " \n",
       ">             Rule[MultilineFunction, None]]}], \"-\", \n",
       "\n",
       " \n",
       ">          RowBox[{FractionBox[RowBox[{\"∂\", RowBox[{\"x\", \"(\", \"t\", \")\"}]}], \n",
       "\n",
       " \n",
       ">             RowBox[{\"∂\", \"t\"}], Rule[MultilineFunction, None]], \" \", \n",
       "\n",
       " \n",
       ">            FractionBox[RowBox[{SuperscriptBox[\"∂\", \"2\"], \n",
       "\n",
       " \n",
       ">               RowBox[{\"L\", \"(\", RowBox[{\"t\", \",\", RowBox[{\"x\", \"(\", \"t\", \")\"}], \",\", \n",
       "\n",
       " \n",
       ">                   RowBox[{SuperscriptBox[\"x\", \"′\", Rule[MultilineFunction, None]], \n",
       "\n",
       " \n",
       ">                     \"(\", \"t\", \")\"}]}], \")\"}]}], \n",
       "\n",
       " \n",
       ">             RowBox[{RowBox[{\"∂\", RowBox[{\"x\", \"(\", \"t\", \")\"}]}], \" \", \n",
       "\n",
       " \n",
       ">               RowBox[{\"∂\", RowBox[{SuperscriptBox[\"x\", \"′\", \n",
       "\n",
       " \n",
       ">                    Rule[MultilineFunction, None]], \"(\", \"t\", \")\"}]}]}], \n",
       "\n",
       " \n",
       ">             Rule[MultilineFunction, None]]}], \"-\", \n",
       "\n",
       " \n",
       ">          RowBox[{FractionBox[RowBox[{SuperscriptBox[\"∂\", \"2\"], \n",
       "\n",
       " \n",
       ">               RowBox[{\"x\", \"(\", \"t\", \")\"}]}], \n",
       "\n",
       " \n",
       ">             RowBox[{\"∂\", SuperscriptBox[\"t\", \"2\"]}], Rule[MultilineFunction, None]], \n",
       "\n",
       " \n",
       ">            \" \", FractionBox[RowBox[{SuperscriptBox[\"∂\", \"2\"], \n",
       "\n",
       " \n",
       ">               RowBox[{\"L\", \"(\", RowBox[{\"t\", \",\", RowBox[{\"x\", \"(\", \"t\", \")\"}], \",\", \n",
       "\n",
       " \n",
       ">                   RowBox[{SuperscriptBox[\"x\", \"′\", Rule[MultilineFunction, None]], \n",
       "\n",
       " \n",
       ">                     \"(\", \"t\", \")\"}]}], \")\"}]}], \n",
       "\n",
       " \n",
       ">             RowBox[{\"∂\", SuperscriptBox[RowBox[{SuperscriptBox[\"x\", \"′\", \n",
       "\n",
       " \n",
       ">                   Rule[MultilineFunction, None]], \"(\", \"t\", \")\"}], \"2\"]}], \n",
       "\n",
       " \n",
       ">             Rule[MultilineFunction, None]]}], \"+\", \n",
       "\n",
       " \n",
       ">          FractionBox[RowBox[{\"∂\", RowBox[{\"L\", \"(\", \n",
       "\n",
       " \n",
       ">               RowBox[{\"t\", \",\", RowBox[{\"x\", \"(\", \"t\", \")\"}], \",\", \n",
       "\n",
       " \n",
       ">                 RowBox[{SuperscriptBox[\"x\", \"′\", Rule[MultilineFunction, None]], \"(\", \n",
       "\n",
       " \n",
       ">                   \"t\", \")\"}]}], \")\"}]}], RowBox[{\"∂\", RowBox[{\"x\", \"(\", \"t\", \")\"}]}], \n",
       "\n",
       " \n",
       ">           Rule[MultilineFunction, None]]}], \"\", \"0\"}], TraditionalForm]\n"
      ]
     },
     "execution_count": 64,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(*The general Lagrangian L for a 1dim. system is give by*)\n",
    "l=L[t, x[t], D[x[t],t]];\n",
    "(*The Euler equation in its symbolic form*)\n",
    "EulerEqn0[l, x, t, eXpand -> True]//pdConv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-02-03T16:51:03+08:00",
     "start_time": "2021-02-03T08:51:03.218Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><img alt=\"Output\" src=\"\"></div>"
      ],
      "text/plain": [
       "FormBox[RowBox[{RowBox[{RowBox[{\"-\", \n",
       "\n",
       " \n",
       ">            FractionBox[RowBox[{SuperscriptBox[\"∂\", \"2\"], \n",
       "\n",
       " \n",
       ">               RowBox[{\"L\", \"(\", RowBox[{\"t\", \",\", RowBox[{\"x\", \"(\", \"t\", \")\"}], \",\", \n",
       "\n",
       " \n",
       ">                   RowBox[{SuperscriptBox[\"x\", \"′\", Rule[MultilineFunction, None]], \n",
       "\n",
       " \n",
       ">                     \"(\", \"t\", \")\"}]}], \")\"}]}], \n",
       "\n",
       " \n",
       ">             RowBox[{RowBox[{\"∂\", \"t\"}], \" \", \n",
       "\n",
       " \n",
       ">               RowBox[{\"∂\", RowBox[{SuperscriptBox[\"x\", \"′\", \n",
       "\n",
       " \n",
       ">                    Rule[MultilineFunction, None]], \"(\", \"t\", \")\"}]}]}], \n",
       "\n",
       " \n",
       ">             Rule[MultilineFunction, None]]}], \"-\", \n",
       "\n",
       " \n",
       ">          RowBox[{FractionBox[RowBox[{\"∂\", RowBox[{\"x\", \"(\", \"t\", \")\"}]}], \n",
       "\n",
       " \n",
       ">             RowBox[{\"∂\", \"t\"}], Rule[MultilineFunction, None]], \" \", \n",
       "\n",
       " \n",
       ">            FractionBox[RowBox[{SuperscriptBox[\"∂\", \"2\"], \n",
       "\n",
       " \n",
       ">               RowBox[{\"L\", \"(\", RowBox[{\"t\", \",\", RowBox[{\"x\", \"(\", \"t\", \")\"}], \",\", \n",
       "\n",
       " \n",
       ">                   RowBox[{SuperscriptBox[\"x\", \"′\", Rule[MultilineFunction, None]], \n",
       "\n",
       " \n",
       ">                     \"(\", \"t\", \")\"}]}], \")\"}]}], \n",
       "\n",
       " \n",
       ">             RowBox[{RowBox[{\"∂\", RowBox[{\"x\", \"(\", \"t\", \")\"}]}], \" \", \n",
       "\n",
       " \n",
       ">               RowBox[{\"∂\", RowBox[{SuperscriptBox[\"x\", \"′\", \n",
       "\n",
       " \n",
       ">                    Rule[MultilineFunction, None]], \"(\", \"t\", \")\"}]}]}], \n",
       "\n",
       " \n",
       ">             Rule[MultilineFunction, None]]}], \"-\", \n",
       "\n",
       " \n",
       ">          RowBox[{FractionBox[RowBox[{SuperscriptBox[\"∂\", \"2\"], \n",
       "\n",
       " \n",
       ">               RowBox[{\"x\", \"(\", \"t\", \")\"}]}], \n",
       "\n",
       " \n",
       ">             RowBox[{\"∂\", SuperscriptBox[\"t\", \"2\"]}], Rule[MultilineFunction, None]], \n",
       "\n",
       " \n",
       ">            \" \", FractionBox[RowBox[{SuperscriptBox[\"∂\", \"2\"], \n",
       "\n",
       " \n",
       ">               RowBox[{\"L\", \"(\", RowBox[{\"t\", \",\", RowBox[{\"x\", \"(\", \"t\", \")\"}], \",\", \n",
       "\n",
       " \n",
       ">                   RowBox[{SuperscriptBox[\"x\", \"′\", Rule[MultilineFunction, None]], \n",
       "\n",
       " \n",
       ">                     \"(\", \"t\", \")\"}]}], \")\"}]}], \n",
       "\n",
       " \n",
       ">             RowBox[{\"∂\", SuperscriptBox[RowBox[{SuperscriptBox[\"x\", \"′\", \n",
       "\n",
       " \n",
       ">                   Rule[MultilineFunction, None]], \"(\", \"t\", \")\"}], \"2\"]}], \n",
       "\n",
       " \n",
       ">             Rule[MultilineFunction, None]]}], \"+\", \n",
       "\n",
       " \n",
       ">          FractionBox[RowBox[{\"∂\", RowBox[{\"L\", \"(\", \n",
       "\n",
       " \n",
       ">               RowBox[{\"t\", \",\", RowBox[{\"x\", \"(\", \"t\", \")\"}], \",\", \n",
       "\n",
       " \n",
       ">                 RowBox[{SuperscriptBox[\"x\", \"′\", Rule[MultilineFunction, None]], \"(\", \n",
       "\n",
       " \n",
       ">                   \"t\", \")\"}]}], \")\"}]}], RowBox[{\"∂\", RowBox[{\"x\", \"(\", \"t\", \")\"}]}], \n",
       "\n",
       " \n",
       ">           Rule[MultilineFunction, None]]}], \"\", \"0\"}], TraditionalForm]\n"
      ]
     },
     "execution_count": 29,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Clear[L, x, t];\n",
    "EulerEquations[L[t, x[t], D[x[t],t]], x[t], t]//pdConv"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.4 欧拉方程的积分法——变分问题求解举例\n",
    "由于欧拉方程边值问题的解满足的只是变分问题的必要条件，所以我们仍要作进一步判别以确定这个解是否为极值函数. \n",
    "但在实际问题中，极值的存在性往往是在问题给出时就确定了. 在这种情况下，对于必要性的讨论也是很有意义的. \n",
    "如果一个实际问题**存在唯一的极值**，可以肯定这个逗留函数就是极值函数. \n",
    "在下面的讨论中所说的极值函数一般都指的是逗留函数. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.4.1 平面上的短程线\n",
    "平面上两点间距离在直线上最短. 这个问题中，我们想要将弧长最小化. \n",
    "$$\n",
    "\\int\\mathrm{d}s \n",
    "= \\int_{(x_1,y_1)}^{(x_2,y_2)}\\sqrt{\\mathrm{d}x^2 + \\mathrm{d}y^2}\n",
    "= \\boxed{\n",
    "\\int_{x_1}^{x_2}\\sqrt{1+{y'}^2}\\mathrm{d}x}\n",
    "$$\n",
    "其中 $\\mathrm{d}s$ 是平面内的弧长微元. \n",
    "\n",
    "自变量是 $x$，因变量是 $y$，我们有 $L(y,y',x) = \\sqrt{1+{y'}^2}$. 则 $\\partial L/\\partial y=0$，\n",
    "所以由欧拉方程，有\n",
    "$$\n",
    "\\frac{\\mathrm{d}}{\\mathrm{d}x}\\left(\\frac{\\partial L}{\\partial y'}\\right) = 0\n",
    "$$\n",
    "由此可以推出\n",
    "$$\n",
    "\\frac{\\partial L}{\\partial y'} = \\frac{y'}{\\sqrt{1+{y'}^2}} = \\text{const}:=c.\n",
    "$$\n",
    "对上式两边同时平方，得到 ${y'}^2(1-c^2) = c^2 >0$，这就要求 $c^2<1$. 则\n",
    "$$\n",
    "y' = \\frac{c}{\\sqrt{1-c^2}} := m\n",
    "$$\n",
    "对上式两边同时积分，得到 $y=mx+b$，其中 $b$ 是积分常数. \n",
    "我们可以用类似的方法将结果推广到任意度量空间(metric spaces)的曲线. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-02-03T15:57:14+08:00",
     "start_time": "2021-02-03T07:57:14.741Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><img alt=\"Output\" src=\"\"></div>"
      ],
      "text/plain": [
       "    y''[x]\n",
       "--------------- == 0\n",
       "          2 3/2\n",
       "(1 + y'[x] )"
      ]
     },
     "execution_count": 9,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Clear[L, y, x];\n",
    "L = Sqrt[(1+(D[y[x],x])^2)];\n",
    "PlaneGeodesicEqn = Simplify[PowerExpand[EulerEqn0[L, y, x, eXpand -> True]]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-02-03T15:57:51+08:00",
     "start_time": "2021-02-03T07:57:51.214Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><img alt=\"Output\" src=\"\"></div>"
      ],
      "text/plain": [
       "{{y[x] -> C[1] + x C[2]}}"
      ]
     },
     "execution_count": 10,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "DSolve[PlaneGeodesicEqn, y[x], x]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.4.2 最速降线\n",
    "求泛函 $$\n",
    "T[y(\\cdot)]=\\boxed{\n",
    "\\int_{0}^{x_2} \\sqrt{\\frac{1+{y'}^2}{y}}\\mathrm{d}x}\n",
    "$$ 在容许函数类 $$\n",
    "X=\\left\\{y(x)|y(x)\\in C^2[0,x_2],y(0)=0,y(x_2)=y_2\\right\\}\n",
    "$$ 中的最小函数. 即最速降线问题. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-02-03T17:07:18+08:00",
     "start_time": "2021-02-03T09:07:18.795Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><img alt=\"Output\" src=\"\"></div>"
      ],
      "text/plain": [
       "             2\n",
       "    1 + y'[x]  + 2 y[x] y''[x]\n",
       "---------------------------------- == 0\n",
       "                             2 3/2\n",
       "Sqrt[g] Sqrt[y[x]] (1 + y'[x] )"
      ]
     },
     "execution_count": 53,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Clear[L, y, x];\n",
    "L = Sqrt[(1+(D[y[x],x])^2)/(2 g y[x])];\n",
    "BrachistochroneEqn = Simplify[PowerExpand[EulerEqn0[L, y, x, eXpand -> True]]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-02-03T17:30:50+08:00",
     "start_time": "2021-02-03T09:30:50.291Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><img alt=\"Output\" src=\"\"></div>"
      ],
      "text/plain": [
       "                    2\n",
       "          -1 - y'[x]\n",
       "{{y[x] == -----------}}\n",
       "           2 y''[x]"
      ]
     },
     "execution_count": 75,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Solve[BrachistochroneEqn, y[x]]/.Rule->Equal"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "其欧拉方程可以化简为\n",
    "$$\n",
    "\\frac{2y''}{1+y'^2}=-\\frac1y,\n",
    "$$\n",
    "把等式两边都乘以 $y'$，并积分得\n",
    "$$\n",
    "\\ln(1+y'^2)=-\\ln(y)+\\ln K,\n",
    "$$\n",
    "即 $y'^2=K/y-1$，\n",
    "由此得到\n",
    "$$\n",
    "\\sqrt{\\frac{y}{K-y}}\\mathrm{d}y=\\pm\\mathrm{d}x.\n",
    "$$\n",
    "令 $y=\\frac{K}2(1-\\cos{u})$，于是有\n",
    "$\\mathrm{d}y=\\frac{K}2\\sin{u}\\mathrm{d}u$. \n",
    "代入上式，化简后得到\n",
    "$$\n",
    "\\frac{K}2(1-\\cos{u})\\mathrm{d}u=\\pm\\mathrm{d}x,\n",
    "$$\n",
    "对上式积分，得到\n",
    "$$\n",
    "x=\\pm\\frac{K}2(u-\\sin{u})+C.\n",
    "$$\n",
    "因为曲线通过坐标原点，所以有 $C=0$. \n",
    "由此可见，最速降线是旋轮线\n",
    "$$\n",
    "\\left\\{\n",
    "\\begin{aligned}\n",
    "x&=\\frac{K}2(u-\\sin{u}),\\\\\n",
    "y&=\\frac{K}2(1-\\cos{u}),\n",
    "\\end{aligned}\n",
    "\\right.\n",
    "$$\n",
    "其中常数 $K$ 可由这条曲线经过点的另一个点确定. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-02-03T17:10:57+08:00",
     "start_time": "2021-02-03T09:10:57.017Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><img alt=\"Output\" src=\"\"></div>"
      ],
      "text/plain": [
       "{{y[x] -> InverseFunction[-(\n",
       " \n",
       "                        2 C[1]          3 C[1]        Sqrt[#1]             #1\n",
       "            Sqrt[#1] (-E       + #1) + E       ArcSin[--------] Sqrt[1 - -------]\n",
       "                                                        C[1]              2 C[1]\n",
       "                                                       E                 E\n",
       ">           ---------------------------------------------------------------------) & ][x]\n",
       "                                           2 C[1]\n",
       "                                     Sqrt[E       - #1]\n",
       " \n",
       ">     }, {y[x] -> \n",
       " \n",
       ">     InverseFunction[\n",
       " \n",
       "                      2 C[1]          3 C[1]        Sqrt[#1]             #1\n",
       "          Sqrt[#1] (-E       + #1) + E       ArcSin[--------] Sqrt[1 - -------]\n",
       "                                                      C[1]              2 C[1]\n",
       "                                                     E                 E\n",
       ">         --------------------------------------------------------------------- & ][x]}}\n",
       "                                         2 C[1]\n",
       "                                   Sqrt[E       - #1]"
      ]
     },
     "execution_count": 59,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sol = DSolve[{BrachistochroneEqn, y[0]==0}, y[x], x]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-02-03T17:12:42+08:00",
     "start_time": "2021-02-03T09:12:42.732Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><img alt=\"Output\" src=\"\"></div>"
      ],
      "text/plain": [
       "InverseFunction[-(\n",
       " \n",
       "                     2 C[1]          3 C[1]        Sqrt[#1]             #1\n",
       "         Sqrt[#1] (-E       + #1) + E       ArcSin[--------] Sqrt[1 - -------]\n",
       "                                                     C[1]              2 C[1]\n",
       "                                                    E                 E\n",
       ">        ---------------------------------------------------------------------) & ][x]\n",
       "                                        2 C[1]\n",
       "                                  Sqrt[E       - #1]"
      ]
     },
     "execution_count": 60,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "soly = y[x]/.sol[[1]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-02-03T17:14:28+08:00",
     "start_time": "2021-02-03T09:14:28.424Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><img alt=\"Output\" src=\"\"></div>"
      ],
      "text/plain": [
       "                2 C[1]            3 C[1]        Sqrt[y[x]]            y[x]\n",
       "  Sqrt[y[x]] (-E       + y[x]) + E       ArcSin[----------] Sqrt[1 - -------]\n",
       "                                                   C[1]               2 C[1]\n",
       "                                                  E                  E\n",
       "-(---------------------------------------------------------------------------) == x\n",
       "                                   2 C[1]\n",
       "                             Sqrt[E       - y[x]]"
      ]
     },
     "execution_count": 63,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "implicitSol = (Head[soly][[1]][y[x]] == soly[[1]])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "验证参数形式最速降线问题的解. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-02-03T16:57:59+08:00",
     "start_time": "2021-02-03T08:57:59.361Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><img alt=\"Output\" src=\"\"></div>"
      ],
      "text/plain": [
       "FormBox[RowBox[{\"{\", RowBox[{RowBox[{FractionBox[RowBox[{RowBox[\n",
       "\n",
       " \n",
       ">                {SuperscriptBox[\"y\", \"′\", Rule[MultilineFunction, None]], \"(\", \"θ\", \n",
       "\n",
       " \n",
       ">                 \")\"}], \" \", RowBox[{\"(\", \n",
       "\n",
       " \n",
       ">                 RowBox[{SuperscriptBox[RowBox[{SuperscriptBox[\"x\", \"′\", \n",
       "\n",
       " \n",
       ">                       Rule[MultilineFunction, None]], \"(\", \"θ\", \")\"}], \"3\"], \"+\", \n",
       "\n",
       " \n",
       ">                   RowBox[{RowBox[{SuperscriptBox[\"x\", \"′\", \n",
       "\n",
       " \n",
       ">                        Rule[MultilineFunction, None]], \"(\", \"θ\", \")\"}], \" \", \n",
       "\n",
       " \n",
       ">                     RowBox[{\"(\", RowBox[{SuperscriptBox[RowBox[{SuperscriptBox[\n",
       "\n",
       " \n",
       ">                             \"y\", \"′\", Rule[MultilineFunction, None]], \"(\", \"θ\", \n",
       "\n",
       " \n",
       ">                            \")\"}], \"2\"], \"+\", \n",
       "\n",
       " \n",
       ">                         RowBox[{\"2\", \" \", RowBox[{\"y\", \"(\", \"θ\", \")\"}], \" \", \n",
       "\n",
       " \n",
       ">                           RowBox[{SuperscriptBox[\"y\", \"′′\", \n",
       "\n",
       " \n",
       ">                              Rule[MultilineFunction, None]], \"(\", \"θ\", \")\"}]}]}], \n",
       "\n",
       " \n",
       ">                       \")\"}]}], \"-\", \n",
       "\n",
       " \n",
       ">                   RowBox[{\"2\", \" \", RowBox[{\"y\", \"(\", \"θ\", \")\"}], \" \", \n",
       "\n",
       " \n",
       ">                     RowBox[{SuperscriptBox[\"x\", \"′′\", \n",
       "\n",
       " \n",
       ">                        Rule[MultilineFunction, None]], \"(\", \"θ\", \")\"}], \" \", \n",
       "\n",
       " \n",
       ">                     RowBox[{SuperscriptBox[\"y\", \"′\", \n",
       "\n",
       " \n",
       ">                        Rule[MultilineFunction, None]], \"(\", \"θ\", \")\"}]}]}], \")\"}]}], \n",
       "\n",
       " \n",
       ">             RowBox[{\"2\", \" \", SqrtBox[\"2\"], \" \", SuperscriptBox[\"g\", \"2\"], \" \", \n",
       "\n",
       " \n",
       ">               SuperscriptBox[RowBox[{\"y\", \"(\", \"θ\", \")\"}], \"3\"], \" \", \n",
       "\n",
       " \n",
       ">               SuperscriptBox[RowBox[{\"(\", \n",
       "\n",
       " \n",
       ">                  FractionBox[RowBox[{SuperscriptBox[RowBox[{SuperscriptBox[\"x\", \n",
       "\n",
       " \n",
       ">                         \"′\", Rule[MultilineFunction, None]], \"(\", \"θ\", \")\"}],\\\n",
       " \n",
       ">    \"2\"]\\\n",
       ", \"+\", SuperscriptBox[RowBox[{SuperscriptBox[\"y\", \"′\", \n",
       "\n",
       " \n",
       ">                         Rule[MultilineFunction, None]], \"(\", \"θ\", \")\"}], \"2\"]}], \n",
       "\n",
       " \n",
       ">                   RowBox[{\"g\", \" \", RowBox[{\"y\", \"(\", \"θ\", \")\"}]}]], \")\"}], \n",
       "\n",
       " \n",
       ">                RowBox[{\"3\", \"/\", \"2\"}]]}]], \"\", \"0\"}], \",\", \n",
       "\n",
       " \n",
       ">          RowBox[{RowBox[{\"-\", FractionBox[RowBox[{RowBox[{SuperscriptBox[\"x\", \"′\", \n",
       "\n",
       " \n",
       ">                    Rule[MultilineFunction, None]], \"(\", \"θ\", \")\"}], \" \", \n",
       "\n",
       " \n",
       ">                 RowBox[{\"(\", RowBox[{SuperscriptBox[RowBox[{SuperscriptBox[\"x\", \n",
       "\n",
       " \n",
       ">                         \"′\", Rule[MultilineFunction, None]], \"(\", \"θ\", \")\"}], \"3\"], \n",
       "\n",
       " \n",
       ">                     \"+\", RowBox[{RowBox[{SuperscriptBox[\"x\", \"′\", \n",
       "\n",
       " \n",
       ">                          Rule[MultilineFunction, None]], \"(\", \"θ\", \")\"}], \" \", \n",
       "\n",
       " \n",
       ">                       RowBox[{\"(\", \n",
       "\n",
       " \n",
       ">                         RowBox[{SuperscriptBox[RowBox[{SuperscriptBox[\"y\", \"′\", \n",
       "\n",
       " \n",
       ">                              Rule[MultilineFunction, None]], \"(\", \"θ\", \")\"}],\\\n",
       " \n",
       ">    \"2\"]\\\n",
       ", \"+\", RowBox[{\"2\", \" \", RowBox[{\"y\", \"(\", \"θ\", \")\"}], \" \", \n",
       "\n",
       " \n",
       ">                             RowBox[{SuperscriptBox[\"y\", \"′′\", \n",
       "\n",
       " \n",
       ">                              Rule[MultilineFunction, None]], \"(\", \"θ\", \")\"}]}]}], \n",
       "\n",
       " \n",
       ">                         \")\"}]}], \"-\", \n",
       "\n",
       " \n",
       ">                     RowBox[{\"2\", \" \", RowBox[{\"y\", \"(\", \"θ\", \")\"}], \" \", \n",
       "\n",
       " \n",
       ">                       RowBox[{SuperscriptBox[\"x\", \"′′\", \n",
       "\n",
       " \n",
       ">                          Rule[MultilineFunction, None]], \"(\", \"θ\", \")\"}], \" \", \n",
       "\n",
       " \n",
       ">                       RowBox[{SuperscriptBox[\"y\", \"′\", \n",
       "\n",
       " \n",
       ">                          Rule[MultilineFunction, None]], \"(\", \"θ\", \")\"}]}]}], \")\"}]}],\\\n",
       " \n",
       ">    \n",
       "            RowBox[{\"2\", \" \", SqrtBox[\"2\"], \" \", SuperscriptBox[\"g\", \"2\"], \" \", \n",
       "\n",
       " \n",
       ">                 SuperscriptBox[RowBox[{\"y\", \"(\", \"θ\", \")\"}], \"3\"], \" \", \n",
       "\n",
       " \n",
       ">                 SuperscriptBox[RowBox[{\"(\", \n",
       "\n",
       " \n",
       ">                    FractionBox[RowBox[{SuperscriptBox[RowBox[{SuperscriptBox[\n",
       "\n",
       " \n",
       ">                           \"x\", \"′\", Rule[MultilineFunction, None]], \"(\", \"θ\", \")\"}\\\n",
       "\n",
       " \n",
       ">   ], \"2\"], \"+\", SuperscriptBox[RowBox[{SuperscriptBox[\"y\", \"′\", \n",
       "\n",
       " \n",
       ">                           Rule[MultilineFunction, None]], \"(\", \"θ\", \")\"}], \"2\"]}], \n",
       "\n",
       " \n",
       ">                     RowBox[{\"g\", \" \", RowBox[{\"y\", \"(\", \"θ\", \")\"}]}]], \")\"}], \n",
       "\n",
       " \n",
       ">                  RowBox[{\"3\", \"/\", \"2\"}]]}]]}], \"\", \"0\"}]}], \"}\"}], TraditionalForm]\n"
      ]
     },
     "execution_count": 36,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "EulerEquations[Sqrt[(x'[\\[Theta]]^2 + y'[\\[Theta]]^2)/( 2 g y[\\[Theta]])], \n",
    "{x[\\[Theta]], y[\\[Theta]]}, \\[Theta]]//TraditionalForm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-02-03T16:58:03+08:00",
     "start_time": "2021-02-03T08:58:03.852Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><img alt=\"Output\" src=\"\"></div>"
      ],
      "text/plain": [
       "FormBox[RowBox[{\"{\", RowBox[{\"True\", \",\", \"True\"}], \"}\"}], TraditionalForm]"
      ]
     },
     "execution_count": 37,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "% /. {\n",
    "   x -> (k (# - Sin[#]) &),\n",
    "   y -> (k (1 - Cos[#]) &)\n",
    "   } // Simplify"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这个方程是摆线(cycloid)的参数方程. \n",
    "对于沿着 $x$ 轴下半部分滚动的轮子，摆线可以由轮子边缘上一点的轨迹给出. 所以，摆线也称为“旋轮线”. \n",
    "\n",
    "摆线的一个有趣的物理现象是：一个质点从摆线上任意一点 $P$ 无初速度地沿着摆线滑下，会同时到达底部. \n",
    "因此，“等时降线”也是摆线. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.4.4 悬链线与最小旋转面\n",
    "考虑泛函\n",
    "$$\n",
    "\\mathcal{I}[y] = \\int y\\mathrm{~d}s = \n",
    "\\boxed{\n",
    "\\int_{x_1}^{x_2} y\\sqrt{1+{y'}^2}\\mathrm{d}x}\n",
    "$$\n",
    "其中，$y' = \\mathrm{d}y/\\mathrm{d}x$. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-02-03T17:52:18+08:00",
     "start_time": "2021-02-03T09:52:18.233Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><img alt=\"Output\" src=\"\"></div>"
      ],
      "text/plain": [
       "         2\n",
       "1 + y'[x]  - y[x] y''[x]\n",
       "------------------------ == 0\n",
       "              2 3/2\n",
       "    (1 + y'[x] )"
      ]
     },
     "execution_count": 78,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Clear[L, y, x];\n",
    "L = y[x] Sqrt[1+(D[y[x],x])^2];\n",
    "CatenaryEqn = Simplify[PowerExpand[EulerEqn0[L, y, x, eXpand -> True]]]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "注意到，这里 $L = L(y,y') = y\\sqrt{1+{y'}^2}$ 与自变量 $x$ 无关. \n",
    "\n",
    "由导数的乘法公式和欧拉方程，我们有\n",
    "$$\n",
    "\\begin{aligned}\n",
    "\\frac{\\mathrm{d}L}{\\mathrm{d}x} \n",
    "&= \\frac{\\partial L}{\\partial x}+\\frac{\\partial L}{\\partial y}y'+\n",
    "\\frac{\\partial L}{\\partial y'}\\frac{\\mathrm{d}y'}{\\mathrm{d}x} \\\\\n",
    "&= \\frac{\\partial L}{\\partial x}+\\frac{\\partial L}{\\partial y}y'+\n",
    "\\frac{\\mathrm{d}}{\\mathrm{d}x}\\left(\\frac{\\partial L}{\\partial y'}y'\\right)-\n",
    "\\frac{\\mathrm{d}}{\\mathrm{d}x}\\left(\\frac{\\partial L}{\\partial y'}\\right)y'\\\\\n",
    "&= \\frac{\\partial L}{\\partial x}+\\frac{\\partial L}{\\partial y}y'+\n",
    "\\frac{\\mathrm{d}}{\\mathrm{d}x}\\left(\\frac{\\partial L}{\\partial y'}y'\\right)-\n",
    "\\frac{\\partial L}{\\partial y}y'\\\\\n",
    "&= \\frac{\\partial L}{\\partial x}+\n",
    "\\frac{\\mathrm{d}}{\\mathrm{d}x}\\left(\\frac{\\partial L}{\\partial y'}y'\\right)\n",
    "\\end{aligned}\n",
    "$$\n",
    "适当整理，得到\n",
    "$$\n",
    "\\frac{\\partial L}{\\partial x}\n",
    "+\\frac{\\mathrm{d}}{\\mathrm{d}x}\\left(\\frac{\\partial L}{\\partial y'}y'-L\\right)=0\n",
    "$$\n",
    "但又因为 $L$ 不显式地依赖于自变量 $x$，所以我们有欧拉方程的第二形式\n",
    "$$\n",
    "\\frac{\\partial L}{\\partial y'}y'-L=\\text{const}:=-\\alpha\n",
    "$$\n",
    "其中，为了方便我们定义常数为 $-\\alpha$. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-02-03T17:55:26+08:00",
     "start_time": "2021-02-03T09:55:25.917Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><img alt=\"Output\" src=\"\"></div>"
      ],
      "text/plain": [
       "                             y[x]\n",
       "{FirstIntegral[x] -> -(----------------)}\n",
       "                                     2\n",
       "                       Sqrt[1 + y'[x] ]"
      ]
     },
     "execution_count": 79,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "FirstIntegrals[L, y[x], x]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "把 $L$ 代入欧拉方程的第二形式，得到\n",
    "$$\n",
    "y'\\frac{y y'}{\\sqrt{1+{y'}^2}}-y\\sqrt{1+{y'}^2}\n",
    "= \\frac{y {y'}^2 - y\\left(1+{y'}^2\\right)}{\\sqrt{1+{y'}^2}}\n",
    "= \\frac{-y}{\\sqrt{1+{y'}^2}} = -\\alpha\n",
    "$$\n",
    "因此 $(y/\\alpha)^2 = 1+{y'}^2$，$y' = \\sqrt{y^2/\\alpha^2-1}$，\n",
    "分离变量，再积分，得到\n",
    "$$\n",
    "x-x_0 = \\int\\frac{\\mathrm{d}y}{\\sqrt{y^2/\\alpha^2-1}}\n",
    "= \\alpha\\cosh^{-1}\\frac{y}{\\alpha}.\n",
    "$$\n",
    "因此，这两个问题的通解为\n",
    "$$\n",
    "y=\\alpha\\cosh\\left(\\frac{x-x_0}{\\alpha}\\right)\n",
    "$$\n",
    "这被称为“摆线”. 注意到，$y(x)>0$ 意味着 $\\alpha>0$. 曲线经过的两个点 $A(x_1,y_1)$，$B(x_2,y_2)$ \n",
    "可以确定了两个参数 $\\alpha$ 和 $x_0$ 的值. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.4.5 欧拉方程的退化情形\n",
    "在求解最小旋转面问题时，发现在函数不依赖于自变量时欧拉方程可以化为一种更简单的形式（一阶微分方程）. \n",
    "下面总结了一些特殊情形下欧拉方程的求解. \n",
    "\n",
    "1. 当 $L\\left(x(t),\\dot{x}(t),t\\right)$ 不依赖于自变量 $t$，\n",
    "即 $L=L\\left(x(t),\\dot{x}(t)\\right)$【保守系统】，这时有\n",
    "$$\n",
    "\\frac{\\mathrm{d}}{\\mathrm{d}t}\\left(f-\\dot{x}L_{\\dot{x}}\\right) \n",
    "= \\dot{x}\\left(L_{x}-\\dot{x}L_{x\\dot{x}}-\\ddot{x}L_{\\dot{x}\\dot{x}}\\right) = 0\n",
    "$$\n",
    "利用首次积分，便得到 $L-\\dot{x}L_{\\dot{x}}=C_1$. \n",
    "通常称 $H(x,\\dot{x})=\\dot{x}\\left(\\partial L/\\partial{\\dot{x}}\\right)-L$ \n",
    "为 Hamilton 量. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Clear[L, x, t, l]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-02-02T16:06:57+08:00",
     "start_time": "2021-02-02T08:06:57.720Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><img alt=\"Output\" src=\"\"></div>"
      ],
      "text/plain": [
       "FormBox[RowBox[{RowBox[{RowBox[{\"-\", \n",
       "\n",
       " \n",
       ">            RowBox[{FractionBox[RowBox[{\"∂\", RowBox[{\"x\", \"(\", \"t\", \")\"}]}], \n",
       "\n",
       " \n",
       ">               RowBox[{\"∂\", \"t\"}], Rule[MultilineFunction, None]], \" \", \n",
       "\n",
       " \n",
       ">              FractionBox[RowBox[{SuperscriptBox[\"∂\", \"2\"], \n",
       "\n",
       " \n",
       ">                 RowBox[{\"L\", \"(\", RowBox[{RowBox[{\"x\", \"(\", \"t\", \")\"}], \",\", \n",
       "\n",
       " \n",
       ">                     RowBox[{SuperscriptBox[\"x\", \"′\", Rule[MultilineFunction, None]], \n",
       "\n",
       " \n",
       ">                       \"(\", \"t\", \")\"}]}], \")\"}]}], \n",
       "\n",
       " \n",
       ">               RowBox[{RowBox[{\"∂\", RowBox[{\"x\", \"(\", \"t\", \")\"}]}], \" \", \n",
       "\n",
       " \n",
       ">                 RowBox[{\"∂\", RowBox[{SuperscriptBox[\"x\", \"′\", \n",
       "\n",
       " \n",
       ">                      Rule[MultilineFunction, None]], \"(\", \"t\", \")\"}]}]}], \n",
       "\n",
       " \n",
       ">               Rule[MultilineFunction, None]]}]}], \"-\", \n",
       "\n",
       " \n",
       ">          RowBox[{FractionBox[RowBox[{SuperscriptBox[\"∂\", \"2\"], \n",
       "\n",
       " \n",
       ">               RowBox[{\"x\", \"(\", \"t\", \")\"}]}], \n",
       "\n",
       " \n",
       ">             RowBox[{\"∂\", SuperscriptBox[\"t\", \"2\"]}], Rule[MultilineFunction, None]], \n",
       "\n",
       " \n",
       ">            \" \", FractionBox[RowBox[{SuperscriptBox[\"∂\", \"2\"], \n",
       "\n",
       " \n",
       ">               RowBox[{\"L\", \"(\", RowBox[{RowBox[{\"x\", \"(\", \"t\", \")\"}], \",\", \n",
       "\n",
       " \n",
       ">                   RowBox[{SuperscriptBox[\"x\", \"′\", Rule[MultilineFunction, None]], \n",
       "\n",
       " \n",
       ">                     \"(\", \"t\", \")\"}]}], \")\"}]}], \n",
       "\n",
       " \n",
       ">             RowBox[{\"∂\", SuperscriptBox[RowBox[{SuperscriptBox[\"x\", \"′\", \n",
       "\n",
       " \n",
       ">                   Rule[MultilineFunction, None]], \"(\", \"t\", \")\"}], \"2\"]}], \n",
       "\n",
       " \n",
       ">             Rule[MultilineFunction, None]]}], \"+\", \n",
       "\n",
       " \n",
       ">          FractionBox[RowBox[{\"∂\", RowBox[{\"L\", \"(\", \n",
       "\n",
       " \n",
       ">               RowBox[{RowBox[{\"x\", \"(\", \"t\", \")\"}], \",\", \n",
       "\n",
       " \n",
       ">                 RowBox[{SuperscriptBox[\"x\", \"′\", Rule[MultilineFunction, None]], \"(\", \n",
       "\n",
       " \n",
       ">                   \"t\", \")\"}]}], \")\"}]}], RowBox[{\"∂\", RowBox[{\"x\", \"(\", \"t\", \")\"}]}], \n",
       "\n",
       " \n",
       ">           Rule[MultilineFunction, None]]}], \"\", \"0\"}], TraditionalForm]\n"
      ]
     },
     "execution_count": 46,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l=L[x[t],D[x[t],t]];\n",
    "EulerEqn0[l, x, t, eXpand -> True]//pdConv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-02-03T18:00:14+08:00",
     "start_time": "2021-02-03T10:00:14.232Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><img alt=\"Output\" src=\"\"></div>"
      ],
      "text/plain": [
       "FormBox[RowBox[{\"{\", RowBox[{RowBox[{\"FirstIntegral\", \"(\", \"t\", \")\"}], \"\", \n",
       "\n",
       " \n",
       ">          RowBox[{RowBox[{FractionBox[RowBox[{\"∂\", RowBox[{\"x\", \"(\", \"t\", \")\"}]}], \n",
       "\n",
       " \n",
       ">               RowBox[{\"∂\", \"t\"}], Rule[MultilineFunction, None]], \" \", \n",
       "\n",
       " \n",
       ">              FractionBox[RowBox[{\"∂\", \n",
       "\n",
       " \n",
       ">                 RowBox[{\"L\", \"(\", RowBox[{RowBox[{\"x\", \"(\", \"t\", \")\"}], \",\", \n",
       "\n",
       " \n",
       ">                     RowBox[{SuperscriptBox[\"x\", \"′\", Rule[MultilineFunction, None]], \n",
       "\n",
       " \n",
       ">                       \"(\", \"t\", \")\"}]}], \")\"}]}], \n",
       "\n",
       " \n",
       ">               RowBox[{\"∂\", RowBox[{SuperscriptBox[\"x\", \"′\", \n",
       "\n",
       " \n",
       ">                    Rule[MultilineFunction, None]], \"(\", \"t\", \")\"}]}], \n",
       "\n",
       " \n",
       ">               Rule[MultilineFunction, None]]}], \"-\", \n",
       "\n",
       " \n",
       ">            RowBox[{\"L\", \"(\", RowBox[{RowBox[{\"x\", \"(\", \"t\", \")\"}], \",\", \n",
       "\n",
       " \n",
       ">                FractionBox[RowBox[{\"∂\", RowBox[{\"x\", \"(\", \"t\", \")\"}]}], \n",
       "\n",
       " \n",
       ">                 RowBox[{\"∂\", \"t\"}], Rule[MultilineFunction, None]]}], \")\"}]}]}],\\\n",
       " \n",
       ">    \"}\"}], \n",
       "   TraditionalForm]\n"
      ]
     },
     "execution_count": 90,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l=L[x[t],D[x[t],t]];\n",
    "FirstIntegrals[l, x[t], t]//pdConv"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2. 当 $L\\left(x(t),\\dot{x}(t),t\\right)$ 不依赖于因变量 $x$，\n",
    "即 $L=L\\left(t, \\dot{x}(t)\\right)$. \n",
    "由欧拉方程，这时有 $\\mathrm{d}/\\mathrm{d}t L_{\\dot{x}}=0$，\n",
    "得到首次积分(first integral) $L_{\\dot{x}}\\left(t, \\dot{x}(t)\\right) = C_1$，\n",
    "解出 $\\dot{x}=\\phi(t, C_1)$，积分得到 $x = \\int \\phi(t,C_1)\\mathrm{d}t$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-02-02T16:06:57+08:00",
     "start_time": "2021-02-02T08:06:57.720Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><img alt=\"Output\" src=\"\"></div>"
      ],
      "text/plain": [
       "FormBox[RowBox[{RowBox[{RowBox[{FractionBox[RowBox[{SuperscriptBox[\"∂\", \"2\"], \n",
       "\n",
       " \n",
       ">               RowBox[{\"x\", \"(\", \"t\", \")\"}]}], \n",
       "\n",
       " \n",
       ">             RowBox[{\"∂\", SuperscriptBox[\"t\", \"2\"]}], Rule[MultilineFunction, None]], \n",
       "\n",
       " \n",
       ">            \" \", RowBox[{\"(\", RowBox[{\"-\", \n",
       "\n",
       " \n",
       ">                FractionBox[RowBox[{SuperscriptBox[\"∂\", \"2\"], \n",
       "\n",
       " \n",
       ">                   RowBox[{\"L\", \"(\", \n",
       "                  RowBox[{\"t\", \",\", \n",
       "\n",
       " \n",
       ">                       RowBox[{SuperscriptBox[\"x\", \"′\", \n",
       "\n",
       " \n",
       ">                          Rule[MultilineFunction, None]], \"(\", \"t\", \")\"}]}], \")\"}]}], \n",
       "\n",
       " \n",
       ">                 RowBox[{\"∂\", SuperscriptBox[RowBox[{SuperscriptBox[\"x\", \"′\", \n",
       "\n",
       " \n",
       ">                       Rule[MultilineFunction, None]], \"(\", \"t\", \")\"}], \"2\"]}], \n",
       "\n",
       " \n",
       ">                 Rule[MultilineFunction, None]]}], \")\"}]}], \"-\", \n",
       "\n",
       " \n",
       ">          FractionBox[RowBox[{SuperscriptBox[\"∂\", \"2\"], \n",
       "\n",
       " \n",
       ">             RowBox[{\"L\", \"(\", RowBox[{\"t\", \",\", \n",
       "\n",
       " \n",
       ">                 RowBox[{SuperscriptBox[\"x\", \"′\", Rule[MultilineFunction, None]], \"(\", \n",
       "\n",
       " \n",
       ">                   \"t\", \")\"}]}], \")\"}]}], \n",
       "        RowBox[{RowBox[{\"∂\", \"t\"}], \" \", \n",
       "\n",
       " \n",
       ">             RowBox[{\"∂\", RowBox[{SuperscriptBox[\"x\", \"′\", \n",
       "\n",
       " \n",
       ">                  Rule[MultilineFunction, None]], \"(\", \"t\", \")\"}]}]}], \n",
       "\n",
       " \n",
       ">           Rule[MultilineFunction, None]]}], \"\", \"0\"}], TraditionalForm]\n"
      ]
     },
     "execution_count": 48,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l=L[t, D[x[t],t]];\n",
    "EulerEqn0[l, x, t, eXpand -> True]//pdConv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-02-03T18:02:47+08:00",
     "start_time": "2021-02-03T10:02:47.156Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><img alt=\"Output\" src=\"\"></div>"
      ],
      "text/plain": [
       "FormBox[RowBox[{\"{\", RowBox[{RowBox[{\"FirstIntegral\", \"(\", \"x\", \")\"}], \"\", \n",
       "\n",
       " \n",
       ">          RowBox[{\"-\", FractionBox[RowBox[{\"∂\", \n",
       "\n",
       " \n",
       ">               RowBox[{\"L\", \"(\", RowBox[{\"t\", \",\", \n",
       "\n",
       " \n",
       ">                   RowBox[{SuperscriptBox[\"x\", \"′\", Rule[MultilineFunction, None]], \n",
       "\n",
       " \n",
       ">                     \"(\", \"t\", \")\"}]}], \")\"}]}], \n",
       "\n",
       " \n",
       ">             RowBox[{\"∂\", RowBox[{SuperscriptBox[\"x\", \"′\", \n",
       "\n",
       " \n",
       ">                  Rule[MultilineFunction, None]], \"(\", \"t\", \")\"}]}], \n",
       "\n",
       " \n",
       ">             Rule[MultilineFunction, None]]}]}], \"}\"}], TraditionalForm]\n"
      ]
     },
     "execution_count": 93,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l=L[t, D[x[t],t]];\n",
    "FirstIntegrals[l, x[t], t]//pdConv"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3. 当 $L\\left(x(t),\\dot{x}(t),t\\right)$ 不依赖于 $\\dot{x}$，\n",
    "即 $L=L\\left(t, x\\right)$. \n",
    "由欧拉方程，这时有 $L_{x}=0$，即 $L(t,x)$ 也与因变量 $x$ 无关. \n",
    "$L$ 就变成了 $L(t)$，无法确定未知函数 $x$，此情形一般无解. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-02-02T16:07:03+08:00",
     "start_time": "2021-02-02T08:07:03.110Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><img alt=\"Output\" src=\"\"></div>"
      ],
      "text/plain": [
       "FormBox[RowBox[{FractionBox[RowBox[{\"∂\", \n",
       "\n",
       " \n",
       ">           RowBox[{\"L\", \"(\", RowBox[{\"t\", \",\", RowBox[{\"x\", \"(\", \"t\", \")\"}]}], \")\"}]}],\\\n",
       " \n",
       ">    \n",
       "      RowBox[{\"∂\", RowBox[{\"x\", \"(\", \"t\", \")\"}]}], Rule[MultilineFunction, None]],\\\n",
       " \n",
       ">    \n",
       "     \"\", \"0\"}], TraditionalForm]\n"
      ]
     },
     "execution_count": 50,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l=L[t, x[t]];\n",
    "EulerEqn0[l, x, t, eXpand -> True]//pdConv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-02-03T18:03:21+08:00",
     "start_time": "2021-02-03T10:03:21.458Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><img alt=\"Output\" src=\"\"></div>"
      ],
      "text/plain": [
       "FormBox[RowBox[{\"{\", \"}\"}], TraditionalForm]"
      ]
     },
     "execution_count": 96,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l=L[t, x[t]];\n",
    "FirstIntegrals[l, x[t], t]//pdConv"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4. 当 $L\\left(x(t),\\dot{x}(t),t\\right)$ 不依赖于自变量 $t$ 和因变量 $x$，即 $L=L(\\dot{x})$. \n",
    "由欧拉方程，这时有 $L''(\\dot{x})\\ddot{x}=0$. \n",
    "    1. 如果 $L''(\\dot{x})=0$，则$L(\\dot{x})=A\\dot{x}+B$，进一步\n",
    "    $\\int_{t_0}^{t_1}L(\\dot{x})\\mathrm{d}t = A\\cdot[x(t_1)-x(t_0)]+B\\cdot(t_1-t_0)$，\n",
    "    极值与函数 $x(t)$ 有关，这种情况应该舍去，即 $L_{\\dot{x}\\dot{x}}\\ne0$. \n",
    "    2. 如果 $\\ddot{x}(t)=0,\\ \\forall t\\in[t_0,t_1]$，则 $x=C t+D$，其中 $C,D$ 由边界条件所确定. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-02-02T16:07:11+08:00",
     "start_time": "2021-02-02T08:07:11.023Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><img alt=\"Output\" src=\"\"></div>"
      ],
      "text/plain": [
       "FormBox[RowBox[{RowBox[{FractionBox[RowBox[{SuperscriptBox[\"∂\", \"2\"], \n",
       "\n",
       " \n",
       ">             RowBox[{\"x\", \"(\", \"t\", \")\"}]}], RowBox[{\"∂\", SuperscriptBox[\"t\", \"2\"]}], \n",
       "\n",
       " \n",
       ">           Rule[MultilineFunction, None]], \" \", \n",
       "\n",
       " \n",
       ">          RowBox[{\"(\", RowBox[{\"-\", FractionBox[RowBox[{SuperscriptBox[\"∂\", \"2\"], \n",
       "\n",
       " \n",
       ">                 RowBox[{\"L\", \"(\", RowBox[{SuperscriptBox[\"x\", \"′\", \n",
       "\n",
       " \n",
       ">                      Rule[MultilineFunction, None]], \"(\", \"t\", \")\"}], \")\"}]}], \n",
       "\n",
       " \n",
       ">               RowBox[{\"∂\", SuperscriptBox[RowBox[{SuperscriptBox[\"x\", \"′\", \n",
       "\n",
       " \n",
       ">                     Rule[MultilineFunction, None]], \"(\", \"t\", \")\"}], \"2\"]}], \n",
       "\n",
       " \n",
       ">               Rule[MultilineFunction, None]]}], \")\"}]}], \"\", \"0\"}], TraditionalForm]\n"
      ]
     },
     "execution_count": 52,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l=L[D[x[t],t]];\n",
    "EulerEqn0[l, x, t, eXpand -> True]//pdConv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-02-03T18:03:36+08:00",
     "start_time": "2021-02-03T10:03:35.936Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><img alt=\"Output\" src=\"\"></div>"
      ],
      "text/plain": [
       "FormBox[RowBox[{\"{\", RowBox[{RowBox[{RowBox[{\"FirstIntegral\", \"(\", \"x\", \")\"}], \n",
       "\n",
       " \n",
       ">            \"\", RowBox[{\"-\", FractionBox[RowBox[{\"∂\", \n",
       "\n",
       " \n",
       ">                 RowBox[{\"L\", \"(\", RowBox[{SuperscriptBox[\"x\", \"′\", \n",
       "\n",
       " \n",
       ">                      Rule[MultilineFunction, None]], \"(\", \"t\", \")\"}], \")\"}]}], \n",
       "\n",
       " \n",
       ">               RowBox[{\"∂\", RowBox[{SuperscriptBox[\"x\", \"′\", \n",
       "\n",
       " \n",
       ">                    Rule[MultilineFunction, None]], \"(\", \"t\", \")\"}]}], \n",
       "\n",
       " \n",
       ">               Rule[MultilineFunction, None]]}]}], \",\", \n",
       "\n",
       " \n",
       ">          RowBox[{RowBox[{\"FirstIntegral\", \"(\", \"t\", \")\"}], \"\", \n",
       "\n",
       " \n",
       ">            RowBox[{RowBox[{FractionBox[RowBox[{\"∂\", RowBox[{\"x\", \"(\", \"t\", \")\"}]}], \n",
       "\n",
       " \n",
       ">                 RowBox[{\"∂\", \"t\"}], Rule[MultilineFunction, None]], \" \", \n",
       "\n",
       " \n",
       ">                FractionBox[RowBox[{\"∂\", \n",
       "                RowBox[{\"L\", \"(\", \n",
       "\n",
       " \n",
       ">                     RowBox[{SuperscriptBox[\"x\", \"′\", Rule[MultilineFunction, None]], \n",
       "\n",
       " \n",
       ">                       \"(\", \"t\", \")\"}], \")\"}]}], \n",
       "\n",
       " \n",
       ">                 RowBox[{\"∂\", RowBox[{SuperscriptBox[\"x\", \"′\", \n",
       "\n",
       " \n",
       ">                      Rule[MultilineFunction, None]], \"(\", \"t\", \")\"}]}], \n",
       "\n",
       " \n",
       ">                 Rule[MultilineFunction, None]]}], \"-\", \n",
       "\n",
       " \n",
       ">              RowBox[{\"L\", \"(\", FractionBox[RowBox[{\"∂\", \n",
       "\n",
       " \n",
       ">                   RowBox[{\"x\", \"(\", \"t\", \")\"}]}], RowBox[{\"∂\", \"t\"}], \n",
       "\n",
       " \n",
       ">                 Rule[MultilineFunction, None]], \")\"}]}]}]}], \"}\"}], TraditionalForm]\n"
      ]
     },
     "execution_count": 99,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l=L[D[x[t],t]];\n",
    "FirstIntegrals[l, x[t], t]//pdConv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-02-03T15:55:09+08:00",
     "start_time": "2021-02-03T07:55:09.884Z"
    }
   },
   "outputs": [],
   "source": [
    "(*wolfram 语言 传统格式输出转换 ———— 偏导\n",
    "来源: \n",
    "https://blog.wolfram.com/2011/12/15/mathematica-qa-series-converting-to-conventional-mathematical-typesetting/\n",
    "*)\n",
    "pdConv[f_] := \n",
    " TraditionalForm[\n",
    "  f /. Derivative[inds__][g_][vars__] :> \n",
    "    Apply[Defer[D[g[vars], ##]] &, \n",
    "     Transpose[{{vars}, {inds}}] /. {{var_, 0} :> \n",
    "        Sequence[], {var_, 1} :> {var}}]]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Wolfram Language 12.1",
   "language": "Wolfram Language",
   "name": "wolframlanguage12.1"
  },
  "language_info": {
   "codemirror_mode": "mathematica",
   "file_extension": ".m",
   "mimetype": "application/vnd.wolfram.m",
   "name": "Wolfram Language",
   "pygments_lexer": "mathematica",
   "version": "12.0"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {
    "height": "405px",
    "width": "378px"
   },
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": true,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "217px"
   },
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
